struct dict {
int len;
char (*dict0)[MAX_WORD_LEN+1];
char (*dict1)[MAX_WORD_LEN+1];
};
/* the memory allocation */
void createDict(struct dict* myDict)
{
(*myDict).dict0 = malloc((*myDict).len*sizeof(char));
(*myDict).dict1 = malloc((*myDict).len*sizeof(char));
if(((*myDict).dict0==0)||((*myDict).dict1==0))
exit(1);
}
for(int i = 0; i < words; i++)
{
scanf("%s", p_diction->dict0[i]);
scanf("%s", p_diction->dict1[i]);
}
for(int i=0; i<words; i++)
{
printf("%s ", &p_diction->dict0[i]);
printf("%s\n", &p_diction->dict1[i]);
}
p_diction
是一个dict
类型的指针。
我设置words
为 11 并输入以下内容:
one lo
two ba
three li
day night
work eat
great terrible
terrible disaster
A a
start delay
finish never
I you
但是当我printf
检查字符串时,它们会打印以下内容:
one ree
two y
three rk
day eat
work rrible
great terrible
terrible art
A nish
start delay
finish never
I you
关于为什么第一个scanf
能完美地阅读它,而第二个只是从稍后出现的单词中读取随机内容的任何想法?