【发布时间】:2010-03-19 06:21:03
【问题描述】:
我正在编写一个 C 程序,但我的字符数组一直有问题。当我使用prinf打印它时,我不断收到垃圾。这是我打印时得到的一个示例:
t.symbol 处的字符是 Aôÿ¿
tabl[0].symbol 处的字符是 A
tabl[1].symbol 处的 char 是一个
tabl[2].symbol 处的 char 是一个
tabl[3].symbol 处的字符是 d
tabl[4].symbol 处的字符是 e
tabl[5].symbol 处的字符是 f
tabl[6].symbol 处的字符是 g
tabl[7].symbol 处的字符是 h
tabl[8].symbol 处的字符是 i
tabl[9].symbol 处的字符是 x
t[0].symbol 处的字符是 a0AÃ
t[1].symbol 处的字符是 b)@Ã4
t[2].symbol 处的字符是 ckU*
t[3].symbol 处的字符是 Aôÿ¿
t[4].symbol 处的字符是 gØ
有人能告诉我如何摆脱字符数组中的垃圾吗?
这是我的代码
#define MAX 100
#ifndef SYMBSIZE
#define SYMBSIZE 1
#endif
typedef struct tableme
{
char symbol[SYMBSIZE];
int value;
int casenmbr;
int otherinfo;
}tabletype;
int main(int argc, char **argv)
{
tabletype t[MAX];
t[3].symbol[0] = 'A';
t[0].value=1;
t[0].casenmbr = 7;
t[0].otherinfo = 682;
tabletype tabl[MAX];
tabl[0].value = 1;
tabl[0].symbol[0] = 'A';
tabl[1].value = 11;
tabl[1].symbol[0]= 'a';
tabl[2].value = 12;
tabl[2].symbol[0] = 'a';
tabl[3].value = 13;
tabl[3].symbol[0] = 'd';
tabl[4].value = 14;
tabl[4].symbol[0] = 'e';
tabl[5].value = 15;
tabl[5].symbol[0] = 'f';
tabl[6].value = 16;
tabl[6].symbol[0] = 'g';
tabl[7].value = 17;
tabl[7].symbol[0] = 'h';
tabl[8].symbol[0] = 'i';
tabl[9].symbol[0] = 'x';
t[1].symbol[0] = 'b';
t[0].symbol[0]= 'a';
t[2].symbol[0]= 'c';
t[4].symbol[0]= 'g';
printf("char at t.symbol is %s \n", t[3].symbol);
for( x=0;x<10;x++)
{
printf("char at tabl[%d].symbol is %s \n",x, tabl[x].symbol);
}
int j;
for(j = 0; j<5;j++)
{
printf("char at t[%d].symbol is %s \n",j, t[j].symbol);
}
return 0;
}
【问题讨论】: