自定义类型
结构体
结构体是一些值的集合,这些值称为成员变量,每个成员可以是不同的类型。
结构体声明
strut tag
{
member_list;
}variable_list;
例如描述一个学生的信息
struct Stu
{
char name[20];//姓名
int age;//年龄
char sex[5];//性别
char id[20];//学号
};//分号不能丢
特殊的声明
例:匿名结构体类型
struct {
char name[20];//姓名
int age;//年龄
char sex[5];//性别
char id[20];//学号
}x;
结构体成员
结构体的成员可以是标量、数组、指针、甚至是其它结构体
结构体成员访问
- 结构体变量的成员是通过(.)访问的。
- 点操作符接受两个操作数
例:
struct
{
char name[20];//姓名
int age;//年龄
};
struct Stu s;//定义结构体变量
访问结构体成员用以下的方法
struct S s;
strcpy(s.name,"zhangsan");//访问name成员
s.age=20;//访问age成员
- 结构体访问成员得到的是一个指向结构体的指针
访问结构体成员用以下的方法
struct S
{
char name[20];//姓名
int age;//年龄
} s;//定义结构体变量
void print(stuct S* ps)
{
printf("name=%s age=&d\n",(*ps).name,(*ps).age);
printf("name=%s age=&d\n",ps->name,ps->age);
}
结构体的自引用
struct Node
{
int date;
struct Node* next;
};
typedef struct Node
{
int date;
struct Node* next;
} Node;
结构体的不完整声明
struct B;
struct A
{
int _a;
struct B* pb;
};
struct B
{
int _b;
struct A* pa;
};
结构体的定义和初始化
struct Point //类型声明
{
int x;
int y;
}p1;//声明类型的同时定义变量p1
struct Point p2;//定义结构体变量p2
//初始化:定义变量的同时赋初值
struct Stu //类型声明
{
char name[20];//姓名
int age;//年龄
};
struct Stu s = { "zhangsan,29" };//初始
结构体内存对齐
计算结构体大小
例:
int main()
{
struct S1
{
char c1;
int i;
char c2;
};
struct S2
{
char c1;
char c2;
int i;
};
struct S3
{
double d;
char c;
int i;
};
struct S4
{
struct S3 s3;
double d;
};
printf("%d\n", sizeof(struct S1));//12
printf("%d\n",sizeof(struct S2));//8
printf("%d\n", sizeof(struct S3));//16
printf("%d\n", sizeof(struct S4));//24
system("pause");
return 0;
}
图解:
结构体对齐规则
- 第一个成员放在结构体偏移量为0的地址处
- 其它成员对齐到对齐数的整数倍的地址处。对齐数=编译器默认的一个对齐数与该成员大小的较小值。vs中默认的值为8 Linux中的默认值为4
- 结构体的总大小为最大对齐数(每个成员都有一个对齐数)的整数倍
- 如果嵌套了结构体,嵌套的结构体对齐到自己最大对齐数的整数倍,结构体的大小就是所有最大对齐数(含嵌套的结构体的对齐数)的整数倍。
###结构体传参
struct S
{
int data[1000];
int num;
};
struct S s = { {1,2,3,4},1000 };
//结构体传参
void print1(struct S s)
{
printf("%d\n", s.num);
}
//结构体地址传参
void print2(struct S* ps)
{
printf("%d\n", ps->num);
}
int main()
{
print1(s);
print2(&s);
system("pause");
return 0;
}
结构体传参时要传地址
原因是:函数传参是需要压栈,当传递结构体对象时,如果一个结构体过大,参数压栈时系统开销过大,导致性能下降。
位段
- 位段的成员必须int、unsigned int 或signed int。
- 位段成员名后面有一个冒号和数字。
例:
int main()
{
struct A
{
int _a : 2;//表示a需要2个bit位
int _b : 5;
int _c : 10;
int _d : 30;
};
printf("%d\n", sizeof(struct A));//位段A的大小是8
system("pause");
return 0;
}
位段的内存分配
- 位段的空间上是按照需要以4个字节(int)或1个字节(char)的方式来开辟的。
- 位段本身不跨平台。