scanf("%s,%d", p1->name, p1->age)無(wú)法接受age的值
struct?Student?{ ????char?name[30]; ????int?age; ????struct?Student?*next; }; struct?Student?*createStudent()?{ ????struct?Student?*head; ????struct?Student?*p1,?*p2; ????int?n?=?0; ????head?=?NULL; ????p1?=?p2?=?(struct?Student?*)?malloc(sizeof(struct?Student)); ????scanf("%s,%d",?p1->name,?&p1->age); //????//分開(kāi)輸入就沒(méi)有問(wèn)題 //????scanf("%s",?p1->name); //????scanf("%d",?&p1->age); ????while?(p1->age?!=?0)?{ ????????n++; ????????if?(n?==?1)?head?=?p1; ????????else?p2->next?=?p1; ????????p2?=?p1; ????????p1?=?(struct?Student?*)?malloc(sizeof(struct?Student)); ????????scanf("%s,%d",?p1->name,?&p1->age); //????//分開(kāi)輸入就沒(méi)有問(wèn)題 //??????scanf("%s",?p1->name); //??????scanf("%d",?&p1->age); ????} ????//使得末尾指向NULL ????p2->next?=?NULL; ????return?(head); } int?main()?{ ????struct?Student?*student; ????student?=?createStudent(); ????printf("%s,%d",?student->name,?student->age); ????return?0; }
老師的結(jié)構(gòu)體內(nèi)是都是 int,我試了,都是int ,scanf("%d,%d", p1->gender, p1->age)可以接受2個(gè)值,但是一個(gè)char[] 一個(gè)int 就第二個(gè)值接受不到?scanf("%s [這里不要逗號(hào)] %d", p1->name, p1->age) 這樣卻可以接受,好奇奇怪。求解
2019-12-21
因?yàn)閟canf在輸入字符串的時(shí)候,是用空格或者回車(chē)表示結(jié)束,如果這里用逗號(hào),scanf就會(huì)認(rèn)為這個(gè)字符串還沒(méi)輸入完成(認(rèn)為逗號(hào)也是字符串的內(nèi)容)