《C语言程序设计》课程教学资源:第十章 结构体和共用体

第十章结构体与共用体 §10.1结构体 结构体是一种构造数据类型 用途:把不同类型的数据组合成一个整体 自定义数据类型 ★结构体类型定义 合法标识符 可省:无名结构体 trut[结构体名 类型标识符成员名; 类型标识符成员名 成员类型可以是 struct是关键字 基本型或构造型 不能省略
第十章 结构体与共用体 §10.1 结构体 结构体是一种构造数据类型 用途:把不同类型的数据组合成一个整体------- 自定义数据类型 结构体类型定义 struct [结构体名] { 类型标识符 成员名; 类型标识符 成员名; ……………. }; 成员类型可以是 struct是关键字, 基本型或构造型 不能省略 合法标识符 可省:无名结构体

num 2字节 name 20字节 例 struct student sex 1字节 int num age 2字节 char name 201 char sex score 4字节 Int age float score char addr 301 30字节 结构体类型定义描述结构 组织形式不分配内存 结构体类型定义的作用域
例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; name num sex age score addr 2字节 2字节 20字节 1字节 4字节 30字节 … ….. 结构体类型定义描述结构 的组织形式,不分配内存 结构体类型定义的作用域

§10.2结构体变量的定义 ★先定义结构体类型,再定义结构体变量 一般形式 struct结构体名 类型标识符成员名; 米刑 员 Wyl#define STudent struct student 例 struct studen STUDENT int num int num char na char name[20] char sex char sex Int age, Int age float scd float score char add char addr[30 struct student student stul. stu
例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1,stu2; §10.2 结构体变量的定义 先定义结构体类型,再定义结构体变量 ❖一般形式: struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }; struct 结构体名 变量名表列; 例 #define STUDENT struct student STUDENT { int num; char name[20]; char sex; int age; float score; char addr[30]; }; STUDENT stu1,stu2;

★定义结构体类型的同时定义结构体变量 一般形式 struct结构体名 类型标识符成员名 类型标识符成员名; }变量名表列; 例 struct student Int num; char name 201 char sex Int age float score char addr[30] 3stul, Stu2
定义结构体类型的同时定义结构体变量 一般形式: struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }变量名表列; 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2;

★直接定义结构体变量 一般形式 struct 类型标识符成员名 类型标识符成员名 变量名表列; 例 struct int num 用无名结构体直接定义 char name 201 变量只能一次 char sex Int age float score char addr[30] 3stul, Stu2
直接定义结构体变量 一般形式: struct { 类型标识符 成员名; 类型标识符 成员名; ……………. }变量名表列; 例 struct { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; 用无名结构体直接定义 变量只能一次

★说明 ◆结构体类型与结构体变量概念不同 0类型不分配内存; 变量,分配内存 类型不能赋值、存取、运算;变量:可以 ◆结构体可嵌套 ◆结构体成员名与程序中变量名可相同。不会混淆 例 量的作用域与生存期 例 struct student int num char name [201 struct date i int month birthday um name int day hum month acthdayear Int year month dayyear )birthday )st stu
说明 ❖结构体类型与结构体变量概念不同 ⚫类型:不分配内存; 变量:分配内存 ⚫类型:不能赋值、存取、运算; 变量:可以 ❖结构体可嵌套 ❖结构体成员名与程序中变量名可相同,不会混淆 例 struct date ❖结构体类型及变量的作用域与生存期 { int month; int day; int year; }; struct student { int num; char name[20]; struct date birthday; }stu; num name birthday month day year 例 struct student { int num; char name[20]; struct date { int month; int day; int year; }birthday; }stu; num name birthday month day year

§10.3结构体变量的引用 ★引用规则 ◆结构体变量不能整体引用只能引用变量成员 引用方式:结构体变量名,成员名 KilL ctruct, student 上个结构体变量 例 struct studen 例 struct student stul num=10 人 struct st i int num; Int nun char name[201: stul score=85. 5 char na char sex m51三)( struct Int age ui score+ stu2 scope. float score HM19875aamn;(×) Int d char adar [30]; birthday Int 3stul, stu2 both dayyear LuzStuI2 birthday 3 Stul, stu2
§10.3 结构体变量的引用 引用规则 ❖ 结构体变量不能整体引用,只能引用变量成员 ❖可以将一个结构体变量赋值给另一个结构体变量 ❖结构体嵌套时逐级引用 成员(分量)运算符 优先级: 1 结合性:从左向右 引用方式: 结构体变量名.成员名 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; stu1.num=10; stu1.score=85.5; stu1.score+=stu2.score; stu1.age++; 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; printf(“%d,%s,%c,%d,%f,%s\n”,stu1); () stu1={101,“Wan Lin”,‘M’,19,87.5,“DaLian”}; () 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; stu2=stu1; ( ) 例 struct student { int num; char name[20]; struct date { int month; int day; int year; }birthday; }stu1,stu2; num name birthday month day year stu1.birthday.month=12; 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; if(stu1==stu2) …….. ()

§10.4结构体变量的初始化 ★形式一 struct结构体名 类型标识符成员名 类型标识符成员名 struct结构体名结构体变量={初始数据}; 例 struct student int num char name 201 char sex: Int age char addr 30 struct student stul=(112,Wang Lin',M,, 19, 200 Beijing Road)
§10.4 结构体变量的初始化 形式一: struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }; struct 结构体名 结构体变量={初始数据}; 例 struct student { int num; char name[20]; char sex; int age; char addr[30]; }; struct student stu1={112,“Wang Lin”,‘M’,19, “200 Beijing Road”};

★形式二: struct结构体名 类型标识符成员名; 类型标识符成员名; }结构体变量={初始数据}; 例 struct student Int num char name[ 20 char sex Int age char addr301 3 Stul=(112,Wang Lin,M,, 19, 200 Beijing Road)
形式二: struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }结构体变量={初始数据}; 例 struct student { int num; char name[20]; char sex; int age; char addr[30]; }stu1={112,“Wang Lin”,‘M’,19, “200 Beijing Road”};

★形式三: struct 类型标识符成员名; 类型标识符成员名; }结构体变量={初始数据}; 例 struct int num char name[ 20 char sex Int age char addr301 3 Stul=(112,Wang Lin,M,, 19, 200 Beijing Road)
形式三: struct { 类型标识符 成员名; 类型标识符 成员名; ……………. }结构体变量={初始数据}; 例 struct { int num; char name[20]; char sex; int age; char addr[30]; }stu1={112,“Wang Lin”,‘M’,19, “200 Beijing Road”};
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C语言程序设计》课程教学资源:第九章 指针.ppt
- 《C语言程序设计》课程教学资源:第八章 预处理命令.ppt
- 《C语言程序设计》课程教学资源:第七章 函数.ppt
- 《C语言程序设计》课程教学资源:第六章 数组.ppt
- 《C语言程序设计》课程教学资源:第五章 选择结构.ppt
- 《C语言程序设计》课程教学资源:第四章 选择结构.ppt
- 《C语言程序设计》课程教学资源:第三章 顺序程序设计.ppt
- 《C语言程序设计》课程教学资源:第二章 数据类型.ppt
- 《C语言程序设计》课程教学资源:第一章 概述.ppt
- 《ASP实用技术》第1章 网络数据库应用系统概述.ppt
- 《ASP实用技术》第7章 ASP中的 Activex组件.ppt
- 《ASP实用技术》第6章 SQL与AD0组件模型.ppt
- 《ASP实用技术》第5章 ASP对象.ppt
- 《ASP实用技术》第4章 ASP技术基础.ppt
- 《ASP实用技术》第3章 客户端脚本语言.ppt
- 《ASP实用技术》第2章 超文本标记语言(HTML).ppt
- 《ASP实用技术》第8章 网络数据库应用系统集成.ppt
- 南京工业大学:《计算机编译原理》(第二版) 第八章 代码优化.ppt
- 南京工业大学:《计算机编译原理》(第二版) 第六章 语义分析与目标代码生成.ppt
- 南京工业大学:《计算机编译原理》(第二版) 第五章 语法分析——-自底向上分析技术.ppt
- 《C语言程序设计》课程教学资源:电子教案.doc
- 《算法分析与设计》讲义(郭彦宏).ppt
- 《计算机通信与计算机网络》电子书.doc
- 《ASP程序设计及应用》第10章 ADO对象.ppt
- 《ASP程序设计及应用》第11章 Web数据库的操作.ppt
- 《ASP程序设计及应用》第12章 设计实例.ppt
- 《ASP程序设计及应用》第1章 ASP基础.ppt
- 《ASP程序设计及应用》第2章 Web页面制作基础.ppt
- 《ASP程序设计及应用》第3章 VBScript脚本语言.ppt
- 《ASP程序设计及应用》第4章 Request和Response对象.ppt
- 《ASP程序设计及应用》第5章 Session和Application对象.ppt
- 《ASP程序设计及应用》第6章 Server和ObjectContext对象.ppt
- 《ASP程序设计及应用》第7章 ASP组件.ppt
- 《ASP程序设计及应用》第8章 文件系统操作.ppt
- 《ASP程序设计及应用》第9章 Web数据库基础.ppt
- 《办公自动化》课程教学资源:第五章 Excel表格处理软件.ppt
- 《办公自动化》课程教学资源:第五章(5-2-3)工作表的修改.ppt
- 《办公自动化》课程教学资源:Exce2000实例祥解.ppt
- 《办公自动化》课程教学资源:第六章 PowerPoint文档演示制作软件.ppt
- 《办公自动化》课程教学资源:计算机基础练习题.doc