我试图在盒子形式中使用printf打印2D结构阵列。每个结构都包含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
答案 0 :(得分:1)
好的,所以我明白了。
在这个循环中:
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';
单引号。
感谢任何花时间看这个的人。