第11章结构体和共用体.ppt

第11章结构体和共用体.ppt

ID:61836582

大小:123.00 KB

页数:53页

时间:2020-02-07

第11章结构体和共用体.ppt_第1页
第11章结构体和共用体.ppt_第2页
第11章结构体和共用体.ppt_第3页
第11章结构体和共用体.ppt_第4页
第11章结构体和共用体.ppt_第5页
资源描述:

《第11章结构体和共用体.ppt》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、§11.1概述C语言的数据类型:基本类型:intcharfloatdouble指针类型构造类型:数组、结构体、共用体枚举类型空类型void结构体(structure)是一种数据类型,它把互相联系的数据组合成一个整体。例、一个学生的学号、姓名、性别、年龄、成绩、地址,是互相联系的数据,在C语言中用“结构体(structure)”来定义。structstudent{   int  num;   /*学号*/   char name[20]; /*姓名*/   char sex;   /*性别*/   int age;  /*年龄*/   float score; 

2、 /*成绩*/   char addr[30]; /*地址*/  };§11.2结构体变量的定义结构体是一种数据类型(像int、char、flaot数据类型一样),可以用它定义变量。用结构体类型定义变量的方式有三种:一、先定义结构体类型,再定义变量例、structstudent  {   int  num;char name[20];char sex;      int age;      float score;     char addr[30];  };structstudentstudent1,student2;结构体变量中的各成员,在内存中顺序存放。

3、结构体变量所占内存大小用运算符sizeof计算。例、printf("%d%d",sizeof(structstudent),sizeof(student1));为了方便,可以这样定义结构体变量:#defineSTUDENTstructstudentSTUDENT{   int  num;      char name[20];    char sex;      int age;      float score;     char addr[30];   };STUDENTstudent1,student2;二、在定义类型的同时定义变量structstu

4、dent  {   int  num;      char name[20];    char sex;      int age;float score;     char addr[30];   }student1,student2;三、直接定义变量struct  {   int  num;      char name[20];char sex;      int age;      float score;    char addr[30];   }student1,student2;四、成员是另一个结构体变量structdate  {    intm

5、onth;intday;      intyear;  };structstudent  {   int  num;char name[20];    char sex;      int age;structdatebirthday;char addr[30];   }student1,student2;§11.3结构体变量的引用1、一般情况下,不能将一个结构体变量作为整体来引用,只能引用其中的成员(分量)。引用结构体成员的方式:结构体变量名.成员名.是“成员运算符”例1、printf("%d,%s,%c,%d,%f,%s",student1.num,stu

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

7、t1.num;   student1.name;   student1.birthday.month;   student1.birthday.day;   student1.birthday.year;3、仅在以下两种情况下,可以把结构体变量作为一个整体来访问。(1)结构体变量整体赋值,例、student2=student1;(2)取结构体变量地址,例、printf("%x",&student1);返回本章目录§11.4结构体变量的初始化structstudent  {   longintnum;char name[20];char sex;char add

8、r[20];}a={89031,"Li

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

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

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