81结构体类型与结构体变量 ◆结构体变量的存储空间 ■一个结构体变量占据一块连续的存储空间,依次 存放各个成员。 ■所占存储空间的大小可用seo运算符计算 ◆使用形式: sizeof(变量名)或 sizeof(类型标识符) 【例】 sizeof(stul)或 sizeof(struct student ☆☆第8章结构体、共用体与枚举类型
☆☆ 第8章 结构体、共用体与枚举类型 11 8.1 结构体类型与结构体变量 ❖结构体变量的存储空间 ▪ 一个结构体变量占据一块连续的存储空间,依次 存放各个成员。 ▪ 所占存储空间的大小可用sizeof运算符计算 ❖ 使用形式: sizeof(变量名) 或 sizeof(类型标识符) •【例】sizeof(stu1) 或 sizeof(struct student)
81结构体类型与结构体变量 ◆8.1.4结构体变量的引用 除了两个相同类型结构体变量可以相互整体赋值外, 不能对结构体变量名直接引用,只能对结构体变量中 的成员分别进行引用。 结构体变量中的成员的引用格式: 结构体变量名成员名 【例】 struct student i int num; stulnum=10001: char name 20; strcpy(stul. name,"Zhang); char sex; stul sex='M stul age=19 Int age; float score, stul score=88 3stul, stu2; 不能写成 stul name=“hang ☆☆第8章结构体、共用体与枚举类型
☆☆ 第8章 结构体、共用体与枚举类型 12 8.1 结构体类型与结构体变量 ❖8.1.4 结构体变量的引用 ▪ 除了两个相同类型结构体变量可以相互整体赋值外, 不能对结构体变量名直接引用,只能对结构体变量中 的成员分别进行引用。 ▪ 结构体变量中的成员的引用格式 : 结构体变量名.成员名 stu1.num=10001; strcpy(stu1.name,"zhang"); stu1.sex='M'; stu1.age=19; stu1.score=88; 【例】struct student { int num; char name[20]; char sex; int age; float score; }stu1,stu2; 不能写成stu1.name=“zhang
令不能把结构体变量作为整体进行输入输出 scanf(%d %s%c%d%f ,stu1); X 令正确的引用方式可以是: gets(stul name); scanf(%d%c %d%f", &stul num, &stul sex, &stulage, &stu score) 思考 为什么不写 scanf(“%s”, stulname);? ☆☆第8章结构体、共用体与枚举类型
☆☆ 第8章 结构体、共用体与枚举类型 13 ❖不能把结构体变量作为整体进行输入输出 scanf("%d%s%c%d%f",stu1); ❖正确的引用方式可以是: gets(stu1.name); scanf("%d%c%d%f",&stu1.num,&stu1.sex,&stu1.age,&stu 1.score); × √ √ 思考: 为什么不写scanf(“%s”,stu1.name);?
81结构体类型与结构体变量 ◆结构体嵌套时逐级引用 s七 ruct date int month int day i struct person personI int year; 引用 personI的出生月份: personlbirthday month struct person char name [201 char sex; struct date birthday i ☆☆第8章结构体、共用体与枚举类型
☆☆ 第8章 结构体、共用体与枚举类型 14 8.1 结构体类型与结构体变量 ❖结构体嵌套时逐级引用 struct date { int month; int day; int year; }; struct person { char name[20]; char sex; struct date birthday; }; struct person person1; 引用person1的出生月份: person1.birthday.month
81结构体类型与结构体变量 例8-2建立一个学生的基本情况表,然后将其打印输 出。 #include <stdio.h> 8.15结构体变量的初始化 #立nc1ude< string.h> ◆结构体变量定义时的初始化形式如下 void main o struct结构体名变量名={各成员值列表}; struct student #include <stdio.h> int num; #include <string.h> char name[20]; void mainO char sex i struct student int age; Int num: f1。 at score char name 20]; Fstul stu2 stu1.num=10001; char sex; strcpy (stul. name,"zha int age float score. stul sex=MI stul age=19 tu2,stul={10001," zhang","M,19,88} stul score=88: stu2=stut; stu2=stu1;//同类结构 printf ("stul: d,%s,C 3 stul num, stul. name, stu1 ☆☆第8章结构体、共用体与枚举类型 15
☆☆ 第8章 结构体、共用体与枚举类型 15 例8-2 建立一个学生的基本情况表,然后将其打印输 出。 #include <stdio.h> #include <string.h> void main() { struct student { int num; char name[20]; char sex; int age; float score; }stu1,stu2; stu1.num=10001; strcpy(stu1.name,"zhang"); stu1.sex='M'; stu1.age=19; stu1.score=88; stu2=stu1; //同类结构体变量之间可以整体赋值 printf("stu1:%d,%s,%c,%6.2f\n", stu1.num,stu1.name,stu1.sex,stu1.score); …… } 8.1 结构体类型与结构体变量 8.1.5 结构体变量的初始化 ❖ 结构体变量定义时的初始化形式如下: struct 结构体名变量名={各成员值列表}; #include <stdio.h> #include <string.h> void main() { struct student { int num; char name[20]; char sex; int age; floatscore; } stu2,stu1={10001,"zhang",'M',19,88}; stu2=stu1; …… }