gpt4 book ai didi

c - 使用 "\n"打印二维结构数组时出现问题

转载 作者:行者123 更新时间:2023-11-30 15:29:19 25 4
gpt4 key购买 nike

我正在尝试使用 printf 以框形式打印二维结构数组。每个结构体都包含一个 int 和 char 值。当我尝试打印它们时,它打印 int 值就很好,但它不是打印 char 值,而是打印 �。当我删除 printf 语句告诉他们在第二个 for 循环(printf("\n")) 完成后转到新行时,它打印得很好(除了它不是盒子形式的事实。这是有些奇怪吗? C 的夸克还是我忽略了某些东西?

for(int i = 0; i < States-1; i++){
for(int j = 0; j < 11; j++){
printf("%d%c ", mArray[i][j].state, mArray[i][j].action);
}
printf("\n");
}

输出如下所示:

99�  99�  99�  99�  99�  99�  99�  99�  99�  99�  99�  
99� 99� 99� 99� 99� 99� 99� 99� 99� 99� 99�
99� 99� 99� 99� 99� 99� 99� 99� 99� 99� 99�
99� 99� 99� 99� 99� 99� 99� 99� 99� 99� 99�

这是我的其余代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "classes.h"

void corUsage(argc)
{
if(argc == 1)
{
printf("usage: ./tokenize tm_file\n");
exit(1);
}

}

int curState = 0;
char curAction = 0;

void maketok(char *str)
{

char *ptr = str;
ptr = strtok(str, "/");
curState = atoi(ptr);
ptr = strtok(NULL, " ");
curAction = ptr;
// printf("%d / %s",curState, curAction);
}

struct Matrix
{
{
int state;
char action;
};

int main(int argc, char *argv[])
{
corUsage(argc);

//opens the file given(argc[1])
FILE *fp;
fp = fopen(argv[1], "r");

//checks for errors
if(fp == NULL)
{//error occured
perror(argv[1]);
exit(1);
}
char buffer[256];//a buffer that holds stuff

char *ptr = buffer;//a pointer to the buffer
char *ptr2;//a pointer for MISC.

int States;//The number of states
int Start;//The starting state
int Accept;//The ending state


//this reads the first three lines of the tm.x file and stores
//it in the variable above.
ptr = fgets(buffer, 256, fp);
ptr2 = strtok(ptr, " ");
ptr2 = strtok(NULL, " ");
States = atoi(ptr2);
ptr = fgets(buffer, 256, fp);
ptr2 = strtok(ptr, " ");
ptr2 = strtok(NULL, " ");
Start = atoi(ptr2);
ptr = fgets(buffer, 256, fp);
ptr2 = strtok(ptr, " ");
ptr2 = strtok(NULL, " ");
Accept = atoi(ptr2);

struct Matrix mArray[States][12];//a 2D array that will be holding structs

while((ptr = fgets(buffer, 256, fp)) != NULL)
{
char *cur = strtok(ptr, " ");
int idx = atoi(cur);
char *buffer2[256];//a buffer for MISC data
int co = 0;//counter vairable that tells how many things are in the line.
for(cur = strtok(NULL, " "); cur != NULL; cur = strtok(NULL, " "))
{
buffer2[co] = cur;//possibily not co as index?
co++;
}
for(int q = 0; q < 12; q++)
{
struct Matrix var;
var.state = 99;
var.action = "d";
mArray[idx][q] = var;
}
/*for(int i = 0; i < co; i++)
{

maketok(buffer2[i]);
printf("%d / %s", curState, curAction);
mArray[idx][i].state = curState;
mArray[idx][i].action = curAction;

}*/

for(int i = 0; i < States-1; i++){
for(int j = 0; j < 11; j++){
printf("%d%c ", mArray[i][j].state, mArray[i][j].action);
}
printf("\n");
}

}

我正在阅读的文件:

states 10
start 0
accept 9
0 0/0d 1/0d 2/1s 3/3s 4/2s 5/2s 6/5s 7/4s 8/4s 10/9d
1 0/9d 1/9d 2/1s 3/1s 4/1s 5/1s 10/9d
2 0/9d 1/9d 3/2s 4/2s 5/2s 10/9d
3 0/9d 1/9d 3/3s 4/3s 5/9d 10/9d
4 0/9d 1/9d 10/9d
5 0/9d 1/9d 7/6s 10/9d
6 0/6s 1/6s 2/6s 3/6s 4/6s 5/6s 6/6s 7/7s 8/6s 9/6s 10/9d
7 0/6s 1/6s 2/6s 3/6s 4/6s 5/6s 6/8s 7/7s 8/6s 9/6s 10/9d
8 0/9d 1/9d 10/9d

最佳答案

好吧,我明白了。

在此循环中:

for(int q = 0; q < 12; q++)
{
struct Matrix var;
var.state = 99;
var.action = "d";
mArray[idx][q] = var;
}

我声明var.action = "d";。它应该是 var.action = 'd'; 仅用单引号引起来。

感谢所有花时间查看此内容的人。

关于c - 使用 "\n"打印二维结构数组时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26309682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com