结构体struct计算机编程C语言

上传人:宝路 文档编号:21465463 上传时间:2017-11-23 格式:DOC 页数:6 大小:42.32KB
返回 下载 相关 举报
结构体struct计算机编程C语言_第1页
第1页 / 共6页
结构体struct计算机编程C语言_第2页
第2页 / 共6页
结构体struct计算机编程C语言_第3页
第3页 / 共6页
结构体struct计算机编程C语言_第4页
第4页 / 共6页
结构体struct计算机编程C语言_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《结构体struct计算机编程C语言》由会员分享,可在线阅读,更多相关《结构体struct计算机编程C语言(6页珍藏版)》请在金锄头文库上搜索。

1、一 结构体 结构体的定义struct student /定义结构提名char name10; /成员列表(包括成员类型,和成员名)int age ;int num;float score; ; /特别注意 不要漏掉次“;”号。二 定义结构体变量方法一;struct student char name10;int age ;int num;float score;student1,student2;/直接在结构体定义的结尾定义结构体变量 student1,student2.方法二struct student char name10;int age ;int num;float score; ;I

2、nt main()struct student student1,student2; /在主函数中定义结构体变量三 成员也可以是一个结构体(也称结构体嵌套)Struct dateInt year;Int month;Int day;struct student char name10;int age ;int num;float score;Struct date birthday; /birthday 是一个(struct date )结构类型。student1,student2; 四 结构体变量的引用方法一结构题变量名.结构体成员名 * 不要忽略两个之间的小点*例如:student1.nu

3、m=10000; *其中的点是分量运算符* 他是所有运算符中优先级最高的例如:student1.birthday.month=6; *这是对结构体嵌套成员引用的方法*注意 结构体变量可以像变量一样进行种运算。也可以通过 scanf 对结构体变量赋值 例如:scanf(%d,student1.num);也可以是:scanf(%d%d%f,student.num,student.age,student1.birthday.year);五,结构体变量的初始化方法一:直接在结构体定义的过程中直接赋值例如:Struct studentint num;Float score;Char name10;stu

4、dent1=10101,78.6,malong,student2=10110,88.6 ,helu ;Printf(num=%d nscore=%fn;name=%sn,student1.num,student1.score,student1.name);方法二:在主函数中进行初始化;#include#includestruct stuint num;char name10;char sex;int main()struct stu stu_1,stu_2; /zstu_1.num=1,strcpy(stu_1.name,malong),stu_1.sex=m;stu_2.num=2,strc

5、py(stu_2.name,haohaibo),stu_2.sex=w;printf(num=%d name=%s sex=%cn,stu_1.num,stu_1.name,stu_1.sex);return 0;总结:在对结构体变量中字符数组成员初始化中,如果采用第二种方法 切记不能指直接stu_2.name=malong; 而是利用字符串函数进行初始化 如上例所示结构体数组所谓结构体数组,就是 在定义结构体变量时定义的是一个结构体数组变量;格式 struct 结构体名 数组名【大小】 ;下来举例说明#includestruct dateint year;int month;int day;

6、struct stuint num;char name10;float score;struct date birthday; /birthday 是一个 struct date 类型的结构体变量stu3=1,malong,78.6,1998,06,01, / 定义一个结构体变量数组,并对其初始化2,haohaibo,88.8,1990,6,3,3,helu,88.9,1990,5,2;int main()int i;for(i=0;i#includestruct personchar name20;int count;leader3=malong,0,haohaibo,0,helu,0;in

7、t main()char name20;int i,j,num;for(i=0;istruct stuint num;float score;int main()struct stu stu_1;stu_1.num=1,stu_1.score=99.8;struct stu *p;p=&stu_1; /切记不要忘了指针指向结构比变量的首地址printf(%d %4.2fn,p-num,p-score); /引用 return 0;总计这里的 p-num 等价于 (*p).num 和 stu_1.num;都是结构体变量成员的引用方式2.指向结构体变量数组的指针。通过名字理解:就是指定义了一个结构

8、体变量数组,然后定义了一个结构体指针变量;结构体变量指针指向结构体数组。就这么简单 关键是记住定义方法 后 OK 了。#includestruct stuint num;char name10;float score;student3=1,malong,88.9,2,haohaibo,99.8,3,helu,88.7; /定义了一个结构体变量数组,并对其初始化int main()struct stu *p; /定义了一个结构体指针 指针指向为结构体 stu 的首地址p=student; / 结构体指针指向结构体变量数组的首地址int i;for(i=0;inum,p-name,p-score)

9、;p+;关于结构体这部分的综合练习:#include#includestruct dateint year;int month;int day;struct stuint num;char name10;char sex;struct date birthday;int main()struct stu student3;struct stu *p;int i;p=student;p-num=1,strcpy(p-name,malong),p-sex=m;p-birthday.year=1996,p-birthday.month=6,p-birthday.day=5;(p+1)-num=2,s

10、trcpy(p+1)-name,liuxiao),(p+1)-sex=w,(p+1)-birthday.year=1992,(p+1)-birthday.month=4,(p+1)-birthday.day=6;(p+2)-num=3,strcpy(p+2)-name,haohaibo),(p+2)-sex=m,(p+2)-birthday.year=1990,(p+2)-birthday.month=3,(p+2)-birthday.day=18;for(i=0;inum,(p+i)-name,(p+i)-sex,(p+i)-birthday.year,(p+i)-birthday.month,(p+i)-birthday.day);return 0;菜鸟的自己的小总结 有错误之处还希望执政 对有编程爱好的一起交流欢迎添加本人 QQ173239336版权所有

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号