结构体struct计算机编程C语言.doc_第1页
结构体struct计算机编程C语言.doc_第2页
结构体struct计算机编程C语言.doc_第3页
结构体struct计算机编程C语言.doc_第4页
结构体struct计算机编程C语言.doc_第5页
已阅读5页,还剩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 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=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;student1=10101,78.6,malong,student2=10110,88.6,helu;Printf(num=%d nscore=%fn;name=%sn,student1.num,student1.score,);方法二:在主函数中进行初始化;#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(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;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;i3;i+)printf(%d %s %4.2f %d %d %dn,stui.num,,stui.score,stui.birthday.year,stui.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;for(i=0;i5;i+)scanf(%s,name);for(j=0;j3;j+)if(strcmp(name,)=0)leaderj.count+;for(i=0;i3;i+)printf(%s %dn,,leaderi.count);return 0;指向结构体类型数据的指针1. 指向结构体变量的指针根据名字自己理解 就是 定义一个结构类型的指针,然后这个指针指向结构体变量结构体指针的定义方法为:Struct 结构体名 *指针变量名;例如:struct stenden *p;/ 这就定义了一个指向结构体student的结构体指针举例说明:再次声明 计算机程序中能采用指针则采用指针,因为运算速度快,所以要好好学习指针原因很简单:指针是对内存地址的操作,不采用指针的话 则每次要先取地址,再取值#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;都是结构体变量成员的引用方式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;/ 结构体指针指向结构体变量数组的首地址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.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.m

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论