}stu1 = {1,”wukong”, ‘m’,99.5f},
stu2 = {2, “bajie”, ‘m’, 65.0f};

typedef
为现有类型创建⼀一个类型别名
语法: typedef 原类型名 新类型名
例如:
typedef int Integer;
Integer a = 10; //等同于 int a = 10;
使⽤用typedef定义结构体
struct student { ...
};
typedef struct student Student;
typedef struct student { ...
}Student;
结构体变量注意事项 可以直接赋值
例如:stu1 = stu2;
注意:数组不可以直接赋值 ⼩小技巧:可以通过把数组放在结构体内实现数组的直接赋值

结构体内存占⽤用
以最⼤大成员变量类型所占空间为分配单位,! 按结构体成员声明顺序⾃自上⽽而下分配。
注:分配空间不⾜足以存储成员变量时,分配新的空间单位。

结构体嵌套 结构体的成员依然可以是结构体。例如:!
typedef struct date{ int year;
int month;
int day; } MyDate;
struct student{
char name[20];
MyDate birthday;//访问:stu1.birthday.year;
};
结构体数组 将多个结构体变量放到数组中,构成结构体数组!
例如:!
struct student students[10] = {0};
可以通过下标访问结构体变量,例如:! students[0].name; // 第⼀一个学⽣生的姓名



总结 结构体是⼀一种⽐比较灵活的数据类型,并且与OC要学的类很相似。结构体和数组的相互嵌套可以实现⽐比较复
543

被折叠的 条评论
为什么被折叠?



