链表C程序错误

链表C程序错误,c,singly-linked-list,C,Singly Linked List,调试链接列表程序时遇到问题。它只是在前几行之后崩溃了,我认为这可能是一个scanf问题,并仔细检查了一遍,但我仍然无法让它运行。它在创建新节点的函数中间崩溃。下面是函数的代码和主要代码 std* CreateNode() { std *newnd; char nm[20]; double g; printf("\nCreating node\n"); printf("\nEnter the student's name:\n"); s

调试链接列表程序时遇到问题。它只是在前几行之后崩溃了,我认为这可能是一个scanf问题,并仔细检查了一遍,但我仍然无法让它运行。它在创建新节点的函数中间崩溃。下面是函数的代码和主要代码

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* newnd = malloc( sizeof *newnd  );
在你的节目里,

std *newnd;
是一个指针,你在哪里分配内存给它的?您使用变量时未在中为其分配内存

 strcpy( (newnd->name), nm);
 newnd->GPA = g;  
 newnd->next = NULL;

这会导致程序崩溃。所以在使用变量之前分配内存。

更好:
std*newnd=(std*)malloc(sizeof(newnd))@JoséX。不要施放malloc的结果。sizeof是一个运算符,不是一个函数。不需要parens@SteveCox这是不正确的。在某些情况下,您需要括号。一个好习惯是总是包含它们。和所有其他操作符一样,您只需要paren来解决操作符的谨慎性。