- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
语言结构体指针初始化.doc
代码如卜*
#include
^include
^include
4.
struct student{
char 氺name;
int score;
struct student氺 next;
} stu, *stul;
10.
int main 0 {
stu. name = (char*)malloc (sizeof (char)) ; /*1.结构体成员指针需 要初始化吟
strepy (stu. name, /zJimy〃);
stu.score = 99;
15.
stul = (struct student*)malloc(sizeof(struct student));/本2?结 构体指针需要初始化*/
stul-name = (char*)malloc(sizeof (char)) ;/*3.结构体指针的成员 指针同样需要初始化*/
stu. next = stul;
strepy (stu卜〉name,Lucy);
stul-〉score = 98;
stul-next = NULL;
printf (name %s, score %d \n 〃,stu. name, stu.score);
printf(〃name %s, score %d \n 〃,stul-〉name,stul-score);
free (stul);
return 0;
}
^include
^include
ftinclude
struct student { char 本name; int score;
struct student* next;
}stu,本stul;
int main() {
stu. name = (char*)malloc (sizeof (char)) ; /*1.结构体成员指针需要初始化
*/
strcpy (stu. name, /zJimy/z); stu.score = 99;
stul = (struct student*)malloc(sizeof(struct student)) ;/*2.结构体指针需要初始化*/
stul-name =
(char*)mal loc(sizeof (char)) ; A3.结构体指针的成员指针同样需要初始化*/ stu.next = stul; strcpy (stul-name, Lucy); stul-score = 98; stul-next = NULL;
printf (z,name %s, score %d \n ,z, stu. name, stu. score); printf (z/name %s, score %d \n 〃,stul-name,stul-scorc); free (stul);
return 0;
}
写测试代码的过程屮我明白了,同事所说的二义树遍历算法屮所用的左子树和A 了树指针不需要初始化,其实是这样的,左了树和右了树指向的必须是二叉树 节点类型的结构体指针(你填?一个长度相M的指针也可以),而该结构体指针是 需要初始化的(见 2),也就是并没有通过malloc來分配内存,而是将另 一个指针的值赋给它
顿时觉得挺无语的,确实,看了很多大学里的教材,对于二叉树的遍历等算法定 义的结构体无非是以下形式
struct node{
int data;
struct node氺 lchild, rchild;
struct node { int data;
struct node* lchild, rchild;
};
使用时都直接的
struct node氺 root;
root = (struct node*)malloc(sizeof(struct node));
root-data = 3;
4.
struct node氺 nlchild;
nlchild = (struct node*)malloc(sizeof(struct node));
root-lchild = nlchild;
nlchild-data = 2;
9.
struct node氺 nrchild;
nlrchild = (struct node*)malloc(sizeof(struct node));
root-rchild = nrchild;
nrchild-〉data = 4;
struct node氺 root;
root = (struct node*)malloc(sizeof(struct node)); root-data = 3;
struct node氺 nlchild;
nlchild = (struct node*)malloc(sizeof(struct node)); root-lchild = nlchild
您可能关注的文档
最近下载
- 人教版二学年上册生字田字格版.doc VIP
- 上教版(试用本)六年级劳动技术全一册电子课本教材.pdf VIP
- 2024年食品安全监管人员专业知识考核试题A卷 附解析.docx VIP
- 2025贵州仟山农林开发(集团)有限公司招聘14人笔试备考试题及答案解析.docx VIP
- 昆明市初三化学初三化学上册期末试卷及答案.doc VIP
- 2025年高校辅导员专业能力综合测试卷.docx VIP
- 2022年四川定向公共基础知识真题(含答案).pdf VIP
- 人教版数学三年级上册第五单元倍的认识单元测试卷(i)卷.doc VIP
- 第4课 社会主义基本经济制度.pptx VIP
- 人教版数学三年级上册第五单元 倍的认识 单元测试卷a卷.doc VIP
原创力文档


文档评论(0)