c语言结构体指针初始化

c语言结构体指针初始化

ID:20377603

大小:91.50 KB

页数:10页

时间:2018-10-11

c语言结构体指针初始化_第1页
c语言结构体指针初始化_第2页
c语言结构体指针初始化_第3页
c语言结构体指针初始化_第4页
c语言结构体指针初始化_第5页
资源描述:

《c语言结构体指针初始化》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、代码如卜*1.#include2.^include3.^include4.4.structstudent{5.char氺name;6.intscore;7.structstudent氺next;8.}stu,*stul;10.9.intmain0{10.stu.name=(char*)malloc(sizeof(char));/*1.结构体成员指针需要初始化吟11.strepy(stu.name,/zJimy〃);12.stu.score=99;15.13.stul=(structstudent*)malloc

2、(sizeof(structstudent));/本2•结构体指针需要初始化*/14.stul->name=(char*)malloc(sizeof(char));/*3.结构体指针的成员指针同样需要初始化*/15.stu.next=stul;16.strepy(stu卜〉name,"Lucy");17.stul-〉score=98;18.stul->next=NULL;19.printf("name%s,score%d〃,stu.name,stu.score);20.printf(〃name%s,scor

3、e%d〃,stul-〉name,stul->score);21.free(stul);22.return0;23.}^include^includeftincludestructstudent{char本name;intscore;structstudent*next;}stu,本stul;intmain(){stu.name=(char*)malloc(sizeof(char));/*1.结构体成员指针需要初始化*/strcpy(stu.name,/zJimy/z);stu.score=99;stul=(

4、structstudent*)malloc(sizeof(structstudent));/*2.结构体指针需要初始化*/stul->name=(char*)malloc(sizeof(char));A3.结构体指针的成员指针同样需要初始化*/stu.next=stul;strcpy(stul->name,"Lucy");stul->score=98;stul->next=NULL;printf(z,name%s,score%d,z,stu.name,stu.score);printf(z/name%s,s

5、core%d〃,stul->name,stul->scorc);free(stul);return0;}写测试代码的过程屮我明白了,同事所说的二义树遍历算法屮所用的左子树和A了树指针不需要初始化,其实是这样的,左了树和右了树指向的必须是二叉树节点类型的结构体指针(你填•一个长度相M的指针也可以),而该结构体指针是需要初始化的(见注释2),也就是并没有通过malloc來分配内存,而是将另一个指针的值赋给它顿时觉得挺无语的,确实,看了很多大学里的教材,对于二叉树的遍历等算法定义的结构体无非是以下形式1.stru

6、ctnode{intdata;structnode氺lchild,rchild;structnode{intdata;structnode*lchild,rchild;};使用时都直接的1.structnode氺root;2.root=(structnode*)malloc(sizeof(structnode));3.root->data=3;4.4.structnode氺nlchild;1.nlchild=(structnode*)malloc(sizeof(structnode));2.root->lchi

7、ld=nlchild;3.nlchild->data=2;9.4.structnode氺nrchild;5.nlrchild=(structnode*)malloc(sizeof(structnode));6.root->rchild=nrchild;7.nrchild-〉data=4;structnode氺root;root=(structnode*)malloc(sizeof(structnode));root->data=3;structnode氺nlchild;nlchild=(structnode*)

8、malloc(sizeof(structnode));root->lchild=nlchild;nlchild~>data=2;structnode氺nrchild;nlrchild=(structnode*)malloc(sizeof(structnode));root->rchild=nrchild;nrchild->data=4;这样子给人造成一种错觉好像结构体成员指针是不用初始

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。