【发布时间】:2017-07-16 13:27:17
【问题描述】:
每当我尝试扫描我的输入时,例如:JASON BOURNE JULY 5 1972,程序就会崩溃。
scanf("%s %s %s %d %d", temp->fname, temp->lname, temp->month, temp->day, temp->year);
我确定它与%d's 而如果我输入:
scanf("%s %s %s", temp->fname, temp->lname, temp->month);
scanf("%d %d", temp->month, temp->day);
String 的值是正确的,程序在 int 被分配之前就崩溃了。
这是我的结构的副本:
typedef struct node{
char fname [29];
char lname [29];
char month [9];
int day;
int year;
struct student * next;
struct student * previous;
} student;
这是我在 main() 中的函数的副本:
student * head = malloc(sizeof(student));
student * temp = head;
int num = numStudents;
while(num != 0){
scanf("%s %s %s %d %d", temp->fname, temp->lname, temp->month, temp->day, temp->year);
printf("FUNCTION NEVER REACHES THIS POINT");
temp = temp->next = malloc(sizeof(student));
num--;
}temp->next = NULL;
【问题讨论】:
-
scanf对%d的期望是什么类型的参数? -
将 & 与变量一起用于 ℅d 说明符。
-
@KyleBatchelor 不,不应该。为什么你认为它应该期望一个整数?
-
如果每个人都使用
gcc -Wall -Werror,那么 50% 标记为c的 Stack Overflow 问题将永远不会被问到。 "scanf 应该期望一个整数值" 是完全错误的。scanf是如何赋值的?4传递给它? -
你的论点根本不是指向。
temp是值,而不是指针。