第9章结构体共用体

第9章结构体共用体

ID:38681262

大小:152.50 KB

页数:14页

时间:2019-06-17

第9章结构体共用体_第1页
第9章结构体共用体_第2页
第9章结构体共用体_第3页
第9章结构体共用体_第4页
第9章结构体共用体_第5页
资源描述:

《第9章结构体共用体》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、第11章结构体与共用体14/14第11章  结构体与共同体11.1概述结构体(structure)是一种数据类型,它把互相联系的数据组合成一个整体。例、一个学生的学号、姓名、性别、年龄、成绩、地址,是互相联系的数据,在C语言中用“结构体(structure)”来定义。structstudent {  int  num;   /*学号*/  char name[20]; /*姓名*/  char sex;   /*性别*/  int age;   /*年龄*/  float score;  /*成绩*/  char addr[30]; /*地

2、址*/ };struct 是关键字,不能省略。student 是定义的结构体类型名。结构体中的每一项数据,称为结构体“成员”(member)或“分量”。“结构体”在大多数资料中被称为“结构”。11.2结构体类型的定义结构体是一种数据类型(像int、char、flaot是数据类型一样),可以用它定义变量。用结构体类型定义变量的方式有三种:1、先定义结构体类型,再定义变量例、structstudent {int  num;   /*学号*/  char name[20]; /*姓名*/  char sex;   /*性别*/  int age;

3、   /*年龄*/  float score;  /*成绩*/  char addr[30]; /*地址*/ };structstudentstudent1,student2;  结构体变量中的各成员,在内存中顺序存放。结构体变量所占内存大小用运算符sizeof计算。它是各各成员所占字节数总和。例、printf("%d%d",sizeof(structstudent),sizeof(student1));为了方便,可以这样定义结构体变量:#defineSTUDENTstructstudentSTUDENT{int  num;     c

4、har name[20];   char sex;     int age;     float score;    char addr[30];  };STUDENTstudent1,student2;第11章结构体与共用体14/142、在定义类型的同时定义变量structstudent {int  num;   /*学号*/  char name[20]; /*姓名*/  char sex;   /*性别*/  int age;   /*年龄*/  float score;  /*成绩*/  char addr[30]; /*地址*/ 

5、}student1,student2;3、直接定义变量struct { int  num;   /*学号*/  char name[20]; /*姓名*/  char sex;   /*性别*/  int age;   /*年龄*/  float score;  /*成绩*/  char addr[30]; /*地址*/ }student1,student2;4、成员是另一个结构体变量structdate/* 日期结构*/ { intmonth;/*月*/   intday;  /*日*/   intyear; /*年*/ };struct

6、student {int  num;   /*学号*/  char name[20]; /*姓名*/  char sex;   /*性别*/  int age;   /*年龄*/  structdatebirthday;/*成员是另一个结构体变量*/  char addr[30]; /*地址*/ }student1,student2;   11.3结构体变量的引用1、一般情况下,不能将一个结构体变量作为整体来引用,只能引用其中的成员(分量)。引用结构体成员的方式:     结构体变量名.成员名.是“成员运算符”(分量运算符)例1、print

7、f("%d,%s,%c,%d,%f,%s",student1.num,student1.name,student1.sex,student1.age,student1.score,sutdent1.addr); 例2、student2.score=student1.score;sum=student1.score+student2.score;  student1.age++;  ++student1.age;例3、scanf("%d",&student1.num);2、当成员是另一个结构体变量时,应一级一级地引用成员。例4、student

8、1.num;  student1.name;  student1.birthday.month;  student1.birthday.day;  student1.birthd

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。