What's wrong with the C language linked list? I don't know how to change it.

-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>
struct student {
    int num;//
    struct student *next;//
};
struct student *crea(int n)
{
    struct student *head, *pa, *pb;
    int i;
    head = NULL;
    for (i = 0; i < n; iPP)
    {
        pb = (struct student*)malloc(sizeof(struct student));
        if (pb == NULL)
        {
            printf("%d!", i + 1);
            break;
        }
    }
    printf(":");
    pb = head;
    scanf_s("%d", &pb->num);
    if (i == 0)
    {
        head = pb;
        pa = pb;
    }
    return head;
}
void  print(struct student *head)
{
    struct student *p = head;
    while(p != NULL)
    {
        printf(":%d", p->num);
        p = p->next;
    }
}
int main()
{
    struct student *head = crea(1);
    print(head);
    system("pause");
    return 0;
}

photos can"t always be uploaded. I don"t know why.
hints that the local pointer variable pb, which may not be initialized, is used

C
Mar.06,2021

  1. change the format, codeshelper uses markdown syntax, and the code can be included using
  2. Please point out what is wrong with you. Whether the compilation failed or ran wrong: if there is a compilation error, please post the error message prompted by the compiler; if it is a run error, please post your input (if any) and expected output
  3. the compiler prompts you that you may use uninitialized variables (this compiler is good) because the for loop may not execute (for example, when n = 0). The following pb = head , because head is not initialized either, the following scanf uses an uninitialized variable pb
  4. gives you a way to write something that should be correct, and compare it with your way of writing
struct student *crea(int n)
{

    struct student *head = NULL, *pa = NULL, *pb;
    int i;
    for (i = 0; i < n; iPP)
    {
        pb = (struct student*)malloc(sizeof(struct student));
        if (pb == NULL)
        {
            printf("%d!", i + 1);
            return NULL;    // 
        }
        printf(":");
        scanf("%d", &pb->num);
        pb->next = NULL;
        if (head == NULL)
        {
            head = pb;
            pa = pb;
        }
        else
            pa->next = pb;
    }
    return head;
}
Menu