11.1.3结构体变量的使用 只有在对结构体变量赋值或作为参数 传递给函数等特殊情况下可以直接对一个 结构体变量整体操作。 其他情况只能对结构体变量的各个 成员分别引用
11.1.3 结构体变量的使用 只有在对结构体变量赋值或作为参数 传递给函数等特殊情况下可以直接对一个 结构体变量整体操作。 其他情况只能对结构体变量的各个 成员分别引用
成员的引用形式为 成员运算符 结构体变量名成员名 例:定义两个变量 struct struct name txll.12; 引用 toll. tel;txk2tli; toll. tel=6543123. tx12tel=8090221: printf( %od", toll. tel)
结构体变量名 .成员名 成员运算符 例: 定义两个变量 struct struct_name txl1,txl2; 引用 txl1.tel; txl2.tel; txl1.tel=6543123; txl2.tel=8090221; printf(“%d”, txl1.tel); 成员的引用形式为:
结构体变量的一般用法: (1)在定义结构体变量的同时,对结拘体变量初始化 struct struct name2 int num char name 8 float score. } studenti={10,“宋红”,89}; 思考:是否可以写成 student1name=宋红” 相当于 student 1. num=10 strcpy( student l name,“宋红”) student l score=89
结构体变量的一般用法: (1) 在定义结构体变量的同时, 对结构体变量初始化 struct struct_name2 { int num; char name[8]; float score; } student1={10, “宋 红”, 89}; 相当于: student1.num=10; strcpy(student1.name, “宋 红”); student1.score=89; 思考:是否可以写成student1.name=“宋 红”;
(2)在些情况下可以对结构体变量的銮体进行操作 例如 struct struct name2 int num, float score 3 studentI, student2; 赋值: student= studentI: 相当于 student2 num=student1 num student2 score=student1 score
(2) 在某些情况下可以对结构体变量的整体进行操作 例如: struct struct_name2 { int num; float score; } student1, student2; 赋值: student2=student1; 相当于: student2.num=student1.num; student2.score=student1.score;
G3)一般来说,在程序设计中不直接引用结构体变量 而是引用结构体变量的某个成员变量。 例如 student2= studentI; 相当于: student2 num=student 1. num student2 score=studenti score 又例如 student1 num=2* student2, num+ student2 num++ sum=studenti score+student2 score
(3) 一般来说, 在程序设计中不直接引用结构体变量, 而是引用 结构体变量的某个成员变量。 又例如: student1.num=2*student2.num+1; student2.num++; sum=student1.score+student2.score; 例如 student2=student1; 相当于: student2.num=student1.num; student2.score=student1.score;