- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
7.1.4 结构指针与链表;1.结构指针;结构指针使用的几种情况:;结构体指针通过指向运算符-
引用结构体中的成员。
p-num=89102;
p-sex=‘F’;
(*p).score=100;;请注意分析下面几种运算:
s-n 得到s指向的结构变量中的成员n的值
s-n++ 得到s指向的结构变量中的成员n的值,用完该值后使它加1
++s-n 得到s指向的结构变量中的成员n的值使之加1;#include stdio.h
struct stu
{ int num;
char name[20];
char sex;
float score;
}student1={1102,Zhang ping,M,78.5},*s;
main()
{ s=student1;
printf(%d\t%s\t,student1.num, student1.name);
printf(%c\t%f\n, student1.sex, student1.score);
printf(%d\t%s\t,(*s).num,(*s).name);
printf(%c\t%f\n,(*s).sex,(*s).score);
printf(%d\t%s\t,s-num,s-name);
printf(%c\t%f\n,s-sex,s-score);
}; struct student stu[3];struct student *p ;
p = stu;;#include stdio.h
struct stu
{ long num;
char name[20];
char sex;
float score;
}student[3]={{200001,Li li,W,99},{200002,Wang hai,M,85},
{200003,Liuying ,W,50}};
main()
{ struct stu *s;
printf(Num\tName\t\tSex\tScore\t\n);
for(s=student;sstudent+3;s++)
printf(%ld\t%-10s\t%c\t%f\t\n,s-num,s-name,s-sex,s-score);
};(3)结构指针作函数参数;for(i=0;i3;i++,ps++)
{ s+=ps-score;
if(ps-score60) n+=1;
}
printf(s=%f\n,s);
ave=s/3;
printf(average=%f\ncount=%d\n,ave,n);
}
main()
{ struct stu *s;
s=student;
average(s);
};2.链表;(1)动态存储分配函数;功能:
在内存的动态存储区中分配一块长度为“size”字节的连续空间。函数的返回值为该空间的首地址;若此函数未能成功的执行,则返回的值为0。; ;功能:
在内存的动态存储区中分配n块长度为“size”字节的连续空间。函数的返回值为该空间的首地址;若此函数未能成功的执行,则返回的值为0。;功能:
释放ptr所指向的内存空间,ptr指向被释放区域的首地址,被释放区应是由malloc或calloc函数所分配的空间。
;#include stdio.h
#include malloc.h
#include string.h
main()
{ struct stu
{ int num;
char name[20];
char sex;
float score;
}*s;
s=(struct stu*)malloc(sizeof(struct stu));
;s-num=1102;
strcpy(s-name,Zhang ping);
s-sex=M;
s-score=92.5;
printf(Number=%d\nName=%s\n,s-num,s-name);
printf(Sex=%c\nScore=%f\n,s-sex,s-score);
free(s);
};(2)链表的使用;; ①链表的建立;#include stdio.h
#include malloc.h
#define NULL 0
#define LEN sizeof (struct stu)
struct stu
{ long num;
float score;
struct stu *next;
}; ;;;num;②链表的输出;void print(struct stu *head )
{ struct stu *p;
printf(N
文档评论(0)