结构定义示例 定义平面坐标结构 定义一个图像 struct point struct image double x dth, he i ght double y; int format pixe Is; 或者 struct point 定义一个产品 struct product double x, y int id it type char name [100] nt price
定义平面坐标结构: struct point { double x; double y; }; 或者 struct point { double x, y; }; 定义一个图像 struct image { int width, height; int format; char * pixels; }; 定义一个产品 struct prooduct { int id; int type; char name[100]; int price; };
结构定义示例 定义一个复数: 定义一个朋友 struct complex struct friend double real, image char name [10] char phone [13] 定义一个地址 Int age struct address struct address addr; char memo [100] char city [20] char street[20] char code: int Zip;
定义一个复数: struct complex { double real, image; }; 定义一个地址 struct address { char city[20]; char street[20]; char code; int zip; }; 定义一个朋友 struct friend { char name[10]; char phone[13]; int age; struct address addr; char memo[100]; };
9.1结构定义和使用 [例9-1]建立一个学生信息库 struct student int num; *学号*/ char name [10] /*姓名*/ Int computer, engl ish,math;/*成绩*/ double average /*平均成绩*/
[例9-1] 建立一个学生信息库 struct student { int num; /*学号*/ char name[10] /*姓名*/ int computer, english, math; /*成绩*/ double average; /*平均成绩*/ };
[例9-1建立一个学生信息厍 #define maxsize 50 struct student students [MaxSize] int count =0 MaxSize是一个宏,定 用结构 struct student 定义了一个数组 students 长度为50 宏定义的一般格式 # define宏名宏体 之后所有的宏名都会被编译器替换为宏体
#define MaxSize 50 struct student students[MaxSize]; int count = 0; MaxSize是一个宏,定义为50 宏定义的一般格式 #define 宏名 宏体 之后所有的宏名都会被编译器替换为宏体 用结构struct student 定义了一个数组students 长度为50