Doubts and Answers

Question 1. Why do pointers need to be used when creating a linked list?

         When I was using a linked list, suddenly, a doubt appeared in my mind, why do I need to use a pointer when creating an empty linked list at the beginning.

#include<stdio.h>


typedef struct student
{

    int num;
    char[10];
    struct student *next;   
 
 }STU;
   

   int main()
{
    STU * stu = (STU *)malloc(sizeof(STU));  




   return 0;
  }

STU * stu = (STU *)malloc(sizeof(STU));  

Analysis: STU * means pointing to a piece of space, and the data type of this space is STU.

if only write

STU * stu ; The space given by the system is uncertain, and there may be values ​​in it (we often call it garbage value). Perhaps an important spatial area of ​​a certain system cannot be changed at will. No access either.

Therefore, you need to allocate your own space in the program. Need to use malloc function

即:(STU *)malloc(sizeof(STU));  

Guess you like

Origin blog.csdn.net/weixin_47783699/article/details/128136427