当我在使用链表的时候,突然,我脑海中出现一个疑惑,为什么开始创建一个空链表时需要用到指针。
- #include
-
-
- 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));
解析:STU * 表示指向一块空间,此空间数据类型为STU型。
如果只写
STU * stu ;系统给的空间是不确定的,里面或许有值(我们常说为垃圾值)。或许某个系统重要的空间区域,不可随意更改。也没有权限访问。
因此,需要在程序中分配属于自己的空间。需要使用malloc函数
即:(STU *)malloc(sizeof(STU));