【发布时间】:2013-03-08 05:47:22
【问题描述】:
struct node {
...
char *name;
...
struct node *next;
};
在递归函数中:
head->name = (char *) malloc(sizeof(char));
if (fscanf(fp, "id\t%d\nname\t%s\nmobile\t%lld\n", &head->id, head->name, &head->mobile) > 0) {
printf("%s\n", head->name);
它存储正确的数据...假设...
鲁特维克
阿比纳夫
但是当打印数据时...
printf("%d\t%s\t%lld\n", head->id, head->name, head->mobile);
1 路特维克 9876655433
2 ��导航 1234567789
让我们暂时搁置 char 指针。 代码与结构配合得很好
struct node {
...
char name[10];
...
struct node *next;
};
但不是当我取名字 [20] 时,它会影响 long long mobile 的价值... 为什么??
输出:
1 鲁特维克 9876655433
2 阿比纳夫 578692242758466
578692242758466 出乎意料。
【问题讨论】:
-
“它存储正确的数据” 从您的输出来看,我怀疑从分配 single char 而不是适当大小的缓冲区开始为您的
name字段。
标签: c linux struct linked-list scanf