制作:方斌 四、结构的成员是另一个结构体变量 struct date/*日期结构*/ int month ; /*K int dayi 月日 int year;/*年*/ } struct student int num; /*学号*/ char s name[20];*姓名*/ char sex /*性别*/ It agei *年龄* struct date birthday;/*成员是另一个结构体变量*/ har addr[30];/*地址*/ student, student2 birthday num name sex age addr month day year
制 作:方 斌 四、结构的成员是另一个结构体变量 struct date /*日期结构 */ { int month; /* 月 */ int day; /* 日 */ int year; /* 年 */ }; struct student { int num; /* 学号 */ char name[20]; /* 姓名 */ char sex; /* 性别 */ int age; /* 年龄 */ struct date birthday; /*成员是另一个结构体变量 */ char addr[30]; /* 地址 */ } student1, student2;
制作:方斌 上例的定义也可以写成如下形式: struct student { int num; /*学号*/ char name[20];/*姓名*/ char sex /*性别*/ int agei /*年龄*/ struct date int year; int month int day birthday /*成员是另一个结构体变量*/ char addr[30]; /*地址*/ student1, student2
制 作:方 斌 上例的定义也可以写成如下形式: struct student { int num; /* 学号 */ char name[20]; /* 姓名 */ char sex; /* 性别 */ int age; /* 年龄 */ struct date { int year; int month; int day; }birthday; /*成员是另一个结构体变量 */ char addr[30]; /* 地址 */ } student1, student2;
[案例111]定义一个反映学生基本情况的结构类型,用以存储学生的相关信息 制作:万 /*将以下案例代码文件存储为文件名 struct. h*/ /*功能:定义一个反映学生基本情况的结构类型* struct date/*日期结构类型:由年、月、日三项组成*/ t int year; int month; int dayi struct std info/*学生信息结构类型:由学号、姓名、性别和生日共4项组成* t char no[7]; char name [ 9] char sex[3l; struct date birthday; }; struct score/*成绩结构类型:由学号和三门成绩共4项组成*/ i char no[7] int score 1 int score2 int score3 };
制 作:方 斌 [案例11.1] 定义一个反映学生基本情况的结构类型,用以存储学生的相关信息。 /*将以下案例代码文件存储为文件名struct.h*/ /*功能:定义一个反映学生基本情况的结构类型*/ struct date /*日期结构类型:由年、月、日三项组成*/ { int year; int month; int day; }; struct std_info /*学生信息结构类型:由学号、姓名、性别和生日共4项组成*/ { char no[7]; char name[9]; char sex[3]; struct date birthday; }; struct score /*成绩结构类型:由学号和三门成绩共4项组成*/ { char no[7]; int score1; int score2; int score3; }; 返回
制作:方斌 11.3结构体变量的引用 1、一般情况下,不能将一个结构体变量作为整体来引用,只能引用其中的 成员(分量)。引用结构体成员的方式: 结构体变量名成员名 是“成员运算符”(分量运算符) 例1、 printf (%/od, %S%C,%d, %f, %s",studentl num, student1 name studentl. sex, student1 age, student1 score, sutdentl. addr) printf(%d%s,%c,%d,%f%s" student1);/*错误* 例2、 student2 score= student1 score; sum= student1 score student2 score; student1 age++ ++student1 age 例3、 scanf(%/d",&student1 num)
制 作:方 斌 11.3 结构体变量的引用 1、一般情况下,不能将一个结构体变量作为整体来引用,只能引用其中的 成员(分量)。引用结构体成员的方式: 结构体变量名.成员名 . 是“成员运算符”(分量运算符) 例1、 printf("%d,%s,%c,%d,%f,%s", student1.num, student1.name, student1.sex,student1.age, student1.score, sutdent1.addr); printf(“%d,%s,%c,%d,%f,%s”, student1); /*错误*/ 例2、 student2.score = student1.score; sum = student1.score + student2.score; student1.age++; ++student1.age; 例3、 scanf("%d", &student1.num);
制作:万斌 2、当成员是另一个结构体变量时,应一级一级地引用成员。例4、 studenti num: student 1, name student birthday month student1 birthday day: student birthday year 3、在某些情况下可以把结构体变量作为一个整体来访问。 (1)错误的引用:结构体变量整体赋值,例、 X student2=student1 (2)取结构体变量地址,例、 printf ("%/ox",&student1); /*输出 student1的地址即第一个成员的地址*
制 作:方 斌 2、当成员是另一个结构体变量时,应一级一级地引用成员。例4、 student1.num; student1.name; student1.birthday.month; student1.birthday.day; student1.birthday.year; 3、在某些情况下可以把结构体变量作为一个整体来访问。 (1) 错误的引用:结构体变量整体赋值,例、 student2 = student1; (2)取结构体变量地址,例、 printf("%x", &student1); /*输出student1的地址即第一个成员的地址*/