scanf("%s,%d", p1->name, p1->age)无法接受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); // //分开输入就没有问题 // 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); // //分开输入就没有问题 // 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; }
老师的结构体内是都是 int,我试了,都是int ,scanf("%d,%d", p1->gender, p1->age)可以接受2个值,但是一个char[] 一个int 就第二个值接受不到 scanf("%s [这里不要逗号] %d", p1->name, p1->age) 这样却可以接受,好奇奇怪。求解