结构体

struct Stu          //6-9是成员列表
{
char name[10];
char tele[10];
char sex[10];
int age;
}s4; //s4全局变量
struct Stu s3; //全局变量
int main()
{
int num;
struct Stu s1;//局部变量
struct Stu s2;
return 0;
}1

匿名结构体

只能用一次

struct
{
int a;
}x;//这能在这里创建结构体

匿名结构体指针

struct
{
int a;
}*px;

结构体自引用

用指针来引用

struct s1
{
int a;
struct s1* next;//next有
}x;

重命名结构体

typedef struct s1
{
int a;
struct s1* next;//next有n
}s1;


 结构体初始化

struct s1         
{
char name[10];
int tele[10];
int age;
}s4;
int main()
{
struct s1 s={ "aaa",100,11};
return 0;
}

结构体引用结构体

struct t
{
double weight;
};
struct s1
{
char name[10];
int tele;
struct t st;
int age;
}s4;
int main()
{
struct s1 s={ "aaa",100,{66.6},11};
printf("%lf", s.st.weight);//引用结构体
return 0;
}

考点:内存对齐

1第一个成员在与结构体偏移量为0的地址处

2其他成员变量对齐到其字节(对齐数)的整数倍的地址

对齐数=编译器和成员大小的较小值(VS 为8)

3结构体总数为最大对齐数的总数倍

4嵌套结构体的对齐到最大对齐数的整数倍

int main()
{
struct s1
{
char a1;
int a;
char a2;
};
}

对齐数为12

int main()
{
struct s1
{
char a1;
char a2;
int a;
};
}

对齐数为8

对齐数最小:对齐数最小的放在最前面,集中放。

修改默认对齐数 

#pragma pack(8)
offsetof(struct s,c)

结构体传参

用地址传参

struct s
{
char a;
};
void x(struct s *ps)
{
ps->a = 'a';
}
void printf1(const struct s* ps)
{
printf("%c", ps->a);
}
int main()
{
struct s s1 = { 0 };
x(&s1);
printf1(&s1);
return 0;
}

位段

二进制位

1位段的成员是int ,unsigned int  ,signed int

2位段成员后面又一个冒号和一个数字

struct s
{
int _a:2;//a需要2个比特位 犹豫30《32 30个字节浪费
int _b:32;b需要33个比特位
};

一共需要8个字节

位段内存开辟

1位段的成员是int ,unsigned int  ,signed int

2位段事以4个字节或者1个字节来开辟的

3位段不可移植