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

上传人:枫** 文档编号:547836096 上传时间:2022-07-22 格式:DOC 页数:6 大小:23.01KB
返回 下载 相关 举报
结构体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; ;Int

2、 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.num=

3、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;studen

4、t1=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,strcpy(st

5、u_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;struct

6、 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;i3;i+)printf(%d %s %4.2f %d %d %dn,stui.num,stui.name,stui.score,stui.birthday.year,s

7、tui.birthday.month,stui.birthday.day); / 对结构体数组成员数组尽享输出,切记这里的stui.num 不能写成stu.num 原因是,前者是结构体变量,后者是结构里变量的地址/ 再举一例 加深理解 要求 对候选人得票统计程序。设3个候选人,每次输入一个候选人的名字,对应的票数加1,最后输出每位候选人即其的票数程序如下:#include#includestruct personchar name20;int count;leader3=malong,0,haohaibo,0,helu,0;int main()char name20;int i,j,num;f

8、or(i=0;i5;i+)scanf(%s,name);for(j=0;j3;j+)if(strcmp(name,leaderj.name)=0)leaderj.count+;for(i=0;i3;i+)printf(%s %dn,leaderi.name,leaderi.count);return 0;指向结构体类型数据的指针1. 指向结构体变量的指针根据名字自己理解 就是 定义一个结构类型的指针,然后这个指针指向结构体变量结构体指针的定义方法为:Struct 结构体名 *指针变量名;例如:struct stenden *p;/ 这就定义了一个指向结构体student的结构体指针举例说明:再

9、次声明 计算机程序中能采用指针则采用指针,因为运算速度快,所以要好好学习指针原因很简单:指针是对内存地址的操作,不采用指针的话 则每次要先取地址,再取值#includestruct 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;都是结构体

10、变量成员的引用方式2. 指向结构体变量数组的指针。通过名字理解:就是指定义了一个结构体变量数组,然后定义了一个结构体指针变量;结构体变量指针指向结构体数组。就这么简单 关键是记住定义方法 后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;/ 结构体指针指向结构体变量数组

11、的首地址int i;for(i=0;inum,p-name,p-score);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.

12、month=6,p-birthday.day=5;(p+1)-num=2,strcpy(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号