2结构体变量的赋值 .结构体变量的赋值、输入和输出 结构体变量的输入和输出也都只能对其成员进行 2同一类型的结构体变量可相互赋值 由于结构体各个成员的类型不同,对结构体变量 赋值也只能对其成员进行 同类型的两个结构体变量之间可以整体赋值 (请比较数组之间不能整体赋值) stud1=stud2
1.结构体变量的赋值、输入和输出 由于结构体各个成员的类型不同,对结构体变量 赋值也只能对其成员进行 结构体变量的输入和输出也都只能对其成员进行 2.同一类型的结构体变量可相互赋值 同类型的两个结构体变量之间可以整体赋值 (请比较数组之间不能整体赋值) stud1=stud2; 2 结构体变量的赋值
【例11-2】输出结构体数据 include <stdio. h> void maino a"E:学习程序 Ctest \Debug\Ctes.口 struct stu input sex and score int num lame=Zhang ping char *name: core=86.000000 ress any key to continue char sex: float score: y boyl, boy2; boyl num=102 boyl. name=Zhang ping" printf(inputsex and scoreIn") scanf(%c %f", &boy l sex, &boy lscore); boy2=boyl printf( Number=%d\nName=%s\n", boy2 num, boy2 name); printf("Sex% Score=%fn", boy2 sex, boy2score);
【例11-2】输出结构体数据 #include <stdio.h> void main() { struct stu { int num; char *name; char sex; float score; } boy1,boy2; boy1.num=102; boy1.name="Zhang ping"; printf("input sex and score\n"); scanf("%c %f",&boy1.sex,&boy1.score); boy2=boy1; printf("Number=%d\nName=%s\n",boy2.num,boy2.name); printf("Sex=%c\nScore=%f\n",boy2.sex,boy2.score); }
稈序分析 本程序中用赋值语句给num和name两个成员 赋值,name是一个字符串指针变量。用 scanf函 数动态地输入sex和 score成员值,然后把boy1的 所有成员的值整体赋予boy2。最后分别输出boy2 的各个成员值。本例表示了结构变量的赋值、输入 和输出的方法
程序分析: 本程序中用赋值语句给num和name两个成员 赋值,name是一个字符串指针变量。用scanf函 数动态地输入sex和score成员值,然后把boy1的 所有成员的值整体赋予boy2。最后分别输出boy2 的各个成员值。本例表示了结构变量的赋值、输入 和输出的方法
1124结构体数组 亻结构体数组的定叉 有三种方法。 (1)先定义结构体类型,用结构体类型名定义结构体数 组,如 struct stud type [char name[20]; long num; int age. char sex; float score; } struct stud type student [50]
11.2.4 结构体数组 1.结构体数组的定义 有三种方法。 (1) 先定义结构体类型,用结构体类型名定义结构体数 组,如: struct stud_type {char name[20]; long num; int age; char sex; float score; }; struct stud_type student[50];
915结构体数组 (2)定义结构体类型名的同时定义结构体数组,如: struct stud type ]student[ 50] (3)不定义结构体类型名,直接定义结构体数组,如 ● struct ]student [50];
9.1.5 结构体数组 (2) 定义结构体类型名的同时定义结构体数组,如: struct stud_type {…… }student[50]; (3) 不定义结构体类型名,直接定义结构体数组,如: struct {…… }student[50];