4、深入结构体
将结构体作为结构体成员
1:
访问方法同上;
注: struct Date my_date; //错误
错误信息说明 Date 结构未定义,如果要使用Date 必须在person 结构体外部定义。
将结构指针用作结构成员
这种类型的结构体主要用在链表上,还有双向链表等,在后面介绍。
结构中的位字段
位字段: 允许定义变量来表示一个整数中的一个活多个位。
注意: 位字段常用在必须节省内存的情况下, 与标准类型的变量相比,位字段会明显降低程序执行速度。
以字长为单位分配内存。
将结构体作为结构体成员
1:
struct Date
{
int day;
int month;
int year;
};
struct Person
{
struct Date birth;
int age;
int height;
char name[20];
char mother[20];
}p1,p2;
访问结构体中的结构体成员p1.birth.day = 14;p1.birth.month = 5;p1.birth.1992;2:struct Person
{
struct Date
{
int day;
int month;
int year;
}birth;
int age;
int height;
char name[20];
char mother[20];
}p1,p2;
访问方法同上;
注: struct Date my_date; //错误
错误信息说明 Date 结构未定义,如果要使用Date 必须在person 结构体外部定义。
将结构指针用作结构成员
struct perosn
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
struct person *next;
};
这种类型的结构体主要用在链表上,还有双向链表等,在后面介绍。
结构中的位字段
位字段: 允许定义变量来表示一个整数中的一个活多个位。
注意: 位字段常用在必须节省内存的情况下, 与标准类型的变量相比,位字段会明显降低程序执行速度。
struct {
unsigned int flag1 :1;
unsigned int flag2 :1;
unsigned int flag3 :2;
unsigned int flag4 :3;
}indicators;
以字长为单位分配内存。