(1)结构体指针是否需要初始化
struct student{
char* name;
int score;
struct student* next;
}stu,*stu1;
stu.name=(char*)malloc(sizeof(char)); //1 结构体成员指针需要初始化
strcpy(stu.name,"Jim");
stu.scor=90;
stu1=(struct student*)malloc(sizeof(struct student)); //2 结构体指针需要初始化
sut1->name=(char*)malloc(sizeof(char)); //3 结构体指针的成员指针同样需要初始化
stu->next=stu1;
strcpy(stu1->name,"Lucy");
stu1->score=80;
stu1->next=NULL;
数据结构中二叉树遍历算法中所用的左子树,右子树看似不用初始化,其实是因为子树必须是二叉节点类型的结构体指针,而该结构体指针是需要初始化的,没有通过malloc来分配,而是将另一个指针的指赋给它。
struct node
{
int data;
struct node* lchild,rchild;
};
struct node* root;
root=(struct node*)malloc(sizeof(struct node));
root-data=3;
struct node* nlchild;
nlchild=(struct node*)malloc(sizeof(strucut node));
root->lchild=nlchild;
nlchild->data=2;
nlchild->lchild=nlchild->rchild=NULL;
struct node* nrchild;
nrchild=(struct node*)malloc(sizeof(struct node));
root->rchild=nrchild;
nrchild->data=4;
nrchild->lchild=nrchild->rchild=NULL;