<分区>
我一直在使用一个非常简单的程序时遇到问题,我真的不知道为什么。有一个人的结构:
typedef struct {
char name[50];
char p_id[11];
char cel[11];
int by;
int id;
} Person;
现在,还有另一个代表联系人列表的结构:
typedef struct {
Person * people;
} lContacts;
我一直在尝试将此人的数据添加到其中,并将此人添加到联系人列表中。加人正常,这里就不贴代码了,但是读取字符串的时候出现了错误:
void include(lContacts * myContacts)
{
Person p;
scanf("%s", p.name);
scanf("%d", &p.by); //birth year
scanf("%s", p.p_id);
printf("TEST P_ID: %s\n\n", p.p_id);
scanf("%s", p.cel);
printf("TEST P_ID AGAIN: %s\n\n", p.p_id);
myContacts->people[index]=p; //don't worry about the index, there is a piece of code I'm omitting to make it easier to read, just assume it is right.
}
}
请注意,我在那里进行了打印测试,因为当我列出我的联系人时,联系人 p_id
本身与 cel
连接在一起,所以我打印了整个代码直到我发现错误就在那里。
这是一个输入示例:
Name
1991
11111111111
<console prints| TEST P_ID: 11111111111>
22222222222
<console prints| TEST P_ID AGAIN: 1111111111122222222222>
但是,如果我打印 p.cel
,它会被正确打印
<console prints 22222222222>
有什么想法吗?也许我应该在扫描字符串时使用“&”? (我读过它和我的理解方式,没有必要..是吗?)