我在调试链接列表程序时遇到问题。它只是在前几行后崩溃,我认为这可能是一个scanf问题,然后再次检查,但仍然无法运行。它在创建新节点的函数中间崩溃。这里是函数的代码和main。链接列表C程序错误
std* CreateNode()
{
std *newnd;
char nm[20];
double g;
printf("\nCreating node\n");
printf("\nEnter the student's name:\n");
scanf("%s", &nm);
printf ("\nEnter the student's GPA:\n");
scanf("%lf", &g);
strcpy((newnd->name), nm);
newnd->GPA = g;
newnd->next = NULL;
return newnd;
}
int main()
{
list_head = (std*) malloc(sizeof(std));
list_tail=(std*) malloc(sizeof(std));
list_tail=(std*) malloc(sizeof(std));
list_head=CreateNode();
A=CreateNode();
B=CreateNode();
C=CreateNode();
PrintList(list_head);
InsertBeg(A);
InsertEnd(B);
InsertMiddle(C);
PrintList(list_head);
SearchStudent();
DeleteMiddle();
DeleteEnd();
DeleteBeg();
PrintList(list_head);
return 0;
}
当我运行程序时,它会在我进入gpa后立即停止执行。
任何帮助将非常欢迎我试过了我能想到的一切。 谢谢! :)
更好:'std * newnd =(std *)malloc(sizeof(newnd));' – zentrunix
@JoséX。不要施放malloc的结果。 http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – clcto
sizeof是一个运算符而不是函数。不需要父母 –