91结控体 9.12结构体变量的定义与初始化 2.结构体变量的初始化 【例95】结构体数组的初始化。 struct s i char num 8l, name 201, sex; float score }stu3={960601l"," Li ming","M',87.5}, f9606012",Zhang jiangguo,M, 79 "9606013", Wang ping",F",90}; 元素的个数可以省略,根据赋初值时 结构体常量的个数确定数组元素的个数 2021/2/21
2021/2/21 11 9.1 结构体 9.1.2 结构体变量的定义与初始化 2. 结构体变量的初始化 【例9.5】结构体数组的初始化。 struct s { char num[8],name[20],sex; float score; }stu[3]={{"9606011","Li ming",'M',87.5}, {"9606012","Zhang jiangguo",'M',79}, {"9606013","Wang ping",'F',90}}; 元素的个数可以省略,根据赋初值时 结构体常量的个数确定数组元素的个数
91结控体 12 9.12结构体变量的定义与初始化 3.结构体变量的运算 ●用szeo运算符计算结构体变量所占内存空间 struct date f int year, month, day: 3; struct student i char num 8, name 20, sex; struct date birthday; float score a sizeof(a)的结果为8+20+1+6+4=39 sizeof(struct student)的结果为39 2021/2/21
2021/2/21 12 9.1 结构体 9.1.2 结构体变量的定义与初始化 3. 结构体变量的运算 ⚫ 用sizeof运算符计算结构体变量所占内存空间 struct date { int year, month, day;}; struct student { char num[8], name[20], sex; struct date birthday; float score; }a; sizeof(a) 的结果为8+20+1+6+4=39 sizeof(struct student) 的结果为39
91结控体 13 9.12结构体变量的定义与初始化 3.结构体变量的运算 ●同类型结构体变量之间的赋值运算 struct date f int year, month, day; 3 struct student i char num 8, name 20l, sex; struct date birthday; file oat score }a={"960601l"," Li ming","M',{1977,12,9},83},b,c; c=a, 结构体变量之间进行赋值时,系统将按成员一一对应赋值。 2021/2/21
2021/2/21 13 9.1 结构体 9.1.2 结构体变量的定义与初始化 3. 结构体变量的运算 ⚫ 同类型结构体变量之间的赋值运算 结构体变量之间进行赋值时,系统将按成员一一对应赋值。 struct date { int year, month, day;}; struct student { char num[8], name[20], sex; struct date birthday; float score; }a={"9606011","Li ming",'M',{1977,12,9},83},b,c; c = a;
91结控体 14 9.12结构体变量的定义与初始化 3.结构体变量的运算 ●对结构体变量进行取址运算 struct date f int year, month, day; 3: struct student i char num 8, name 20, sex; struct date birthday float score: }a; 对结构体变量a进行&a运算,可以得到a的 首地址,它是结构体类型指针 2021/2/21
2021/2/21 14 9.1 结构体 9.1.2 结构体变量的定义与初始化 3. 结构体变量的运算 ⚫ 对结构体变量进行取址运算 struct date { int year, month, day;}; struct student { char num[8], name[20], sex; struct date birthday; float score; }a; 对结构体变量a进行 &a 运算,可以得到a的 首地址,它是结构体类型指针
91结控体 15 912结构体变量的定义与初始化 “”是分量运 4.结构体变量成员的引用 算符,运算级 别最高。 结构体变量成员引用的一般形式 结构体变量名成员名 struct date 结构体变量的各个成员可 进行何种运算,由该成员 f int year, month, day; 3; 的数据类型决定 struct student i char num[8], name(20), sex; a birthday year struct date birthday; a birthday month loat score; abirthdayday }a; 结构体变量a的各成员可分别表示为amm、 aname、a.sex、a. birthday、a., score 022/21
2021/2/21 15 9.1 结构体 9.1.2 结构体变量的定义与初始化 4. 结构体变量成员的引用 结构体变量成员引用的一般形式: 结构体变量名.成员名 结构体变量a的各成员可分别表示为a.num、 a.name、a.sex、a.birthday、a.score struct date { int year, month, day;}; struct student { char num[8], name[20], sex; struct date birthday; float score; }a; “.”是分量运 算符,运算级 别最高。 a.birthday.year a.birthday.month a.birthday.day 结构体变量的各个成员可 进行何种运算,由该成员 的数据类型决定