/yuwenliang/article/details/5678982
C代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
structstudent{
char*name;
intscore;
structstudent*next;
}stu,*stu1;
intmain(){
stu.name=(char*)malloc(sizeof(char));/*1.结构体成员指针需要初始化*/
strcpy(stu.name,"Jimy");
stu.score=99;
stu1=(structstudent*)malloc(sizeof(structstudent));/*2.结构体指针需要初始化*/
stu1->name=(char*)malloc(sizeof(char));/*3.结构体指针的成员指针同样需要初始化*/
stu.next=stu1;
strcpy(stu1->name,"Lucy");
stu1->score=98;
stu1->next=NULL;
printf("name%s,score%d/n",stu.name,stu.score);
printf("name%s,score%d/n",stu1->name,stu1->score);
free(stu1);
return0;
}
写测试代码的过程中我明白了,同事所说的二叉树遍历算法中所用的左子树和右子树指针不需要初始化,其实是这样的,左子树和右子树指向的必须是二叉树节点类型的结构体指针(你填一个长度相同的指针也可以),而该结构体指针是需要初始化的(见注释2),也就是并没有通过malloc来分配内存,而是将另一个指针的值赋给它
顿时觉得挺无语的,确实,看了很多大学里的教材,对于二叉树的遍历等算法定义的结构体无非是以下形式
C代码
structnode{
intdata;
structnode*lchild,rchild;
};
使用时都直接的
C代码
structnode*root;
root=(structnode*)malloc(sizeof(structnode));
root->data=3;
structnode*nlchild;
nlchild=(structnode*)malloc(sizeof(structnode));
root->lchild=nlchild;
nlchild->data=2;
structnode*nrchild;
nlrchild=(structnode*)malloc(sizeof(structnode));
root->rchild=nrchild;
nrchild->data=4;
这样子给人造成一种错觉好像结构体成员指针是不用初始化的。
可是,只要是指针,要使用它前就必须保证指针变量的值是一个有效的值;否则,它指向的内存一定是垃圾数据!
C语言的内存管理很重要,集魄力和麻烦于一身,看你自己的心态如何了。如果你积极的面对,你正在控制一切;如果你觉得烦躁,你正不得不控制一切。C仍旧是博大精深的语言,信C哥!
/*附加:仍旧是指针*/
C代码
stu1=(structstudent*)malloc(sizeof(structstudent));/*2.结构体指针需要初始化*/
这一句可能会有人把sizeof里边也填成struct student*
可以理解这样的行为,因为stu本来就是struct student*,可是这样子你就没有为结构体分配足够的内存,使用中会因为内存错误同样报错的。
当然,仅仅为结构体指针分配内存还不够,结构体成员指针仍然需要分配内存,如下
C代码
stu1->name=(char*)malloc(sizeof(char));