资源描述:
《第10-11章 结构体与其它数据类型 位运算ppt课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第10章结构体结构体的基本概念结构体数组结构体指针问题的提出要描述一个同学的情况,包括:姓名、所在系、出生日期、性别、期末成绩(10门)字符串字符整型属三种类型,无法用一个数组加以描述。一个解决办法是用多个变量表示:charname[10],depart[10],birthday[10];charsex;intscore[10];但这样缺乏一个整体的概念,如果要表示全班所有的同学,简单变量显然更无法表示,使用多个数组在表达意义上又不够清晰。一、结构体定义表示多种类型的数据集合,结构体是最有效的方式。结构
2、体一般定义:struct结构体类型名{成员表1;成员表2;......成员表n;};结构体类型定义同学信息的结构体表示:structstudent{charname[10],depart[10];charbirthday[10];charsex;intscore[10];}x;成员表定义是类型名,而不是变量类型定义不引起内存分配x才是变量};structstudentx;类型名结构体定义的方法先声明结构体类型再定义变量在声明结构体类型的同时定义变量直接定义结构体类型变量struct结构体名{成员
3、表列};struct结构体名变量名;struct结构体名{成员表列}变量名;struct{成员表列}变量名;structdate{intyear;intmonth;intday;};structdatex;structdate{intyear;intmonth;intday;}x;struct{intyear;intmonth;intday;}x;结构体定义结构体变量的赋初值:structstudent{charname[10],depart[10],birthday[10];charse
4、x;intscore[10];}s1={"zhang","compute","1980.01.01",'m',{78,66,89,92,90,88,67}};结构体变量s1的存储表示:内存单元连续存放‘z’‘h’‘a’‘n’‘g’‘ ’‘c’……8867结构体定义再定义教师信息结构体structteach{charname[10],depart[10],birthday[10];charsex;charwork[10];}s3={"zhang","software","1950.01.01",'m',
5、"professor”};structstudent和structteach是两种不同的结构体类型,s1、s3是两种不同类型的变量。结构体定义结构体中的成员可以是基本类型、指针、数组,也可是其它已定义的结构体。structbirth{intyear,month,day;};只定义类型,没有定义变量structstudent{charname[10],depart[10];structbirthbirthday;定义变量charsex;intscore[10];}s1={"zhang","math",{1
6、970,01,01},'m',{78,66,89,92,90,88,67}},s2;结构体定义结构体定义位置structbirth{intyear,month,day;};main(){structstudent{charname[10],depart[10];structbirthbirthday;charsex;};……}与函数相似二、结构体变量的使用结构体变量是一种数据复合体变量,包含有多个数据,其整体操作象数组那样,是受到限制的。整体操作s2=s1;整体赋值是结构体变量的唯一整体操作。结构体变量
7、的其它操作都要按照成员来进行二、结构体变量的使用成员操作(由成员本身类型决定)成员指定:s1.name(字符串型)s1.score[2](数组元素)s1.birthday.year(结构体的成员又是结构体)输入:scanf("%s%d",s1.name,&s1.birthday.month);输出:printf("%s%d",s1.depart,s2.score[1]);结构体的成员能进行其本身类型所允许的所有运算。structstudent{charname[10],depart[10];struct
8、birthbirthday;charsex;intscore[10];}s1结构体变量的使用例:输入一个同学的数据并求其平均分。main(){structstudent{charname[10],depart[10];structbirth{intyear,month,day;}birthday;charsex;intscore[3];intaverage;}s1;inti;s1.average=0;scanf("%s%s",s1.name