结构体
- 结构体类型的定义
(1)定义结构体是定义类型
(2)C语言中结构体成员不可为空
#include <stdio.h>
//结构体类型的定义
typedef struct stu{
char name[20];//名字
char sex[5];//性别
int age;//年龄
char id[20];//学号
}_stu;
struct stu{
char name[20];
char sex[5];
int age;
char id[20];
};
typedef struct stu _stu;
//typedef为C语言的关键字,作用是为一种数据类型定义一个新名字(类型重命名)
//以上两个结构体类型的定义是等价的,_stu相当于sruct stu类型的别名
//为什么要这样定义? 如果不这样写,后面定义结构体类型变量时:struct stu student,不方便
- 结构体变量的定义和初始化
(1)定义结构体变量是定义变量
(2)结构体变量只能被整体初始化,不能被整体赋值(初始化只能在定义变量的同时进行)
(3)在不加结构体标签(名字)的情况下,两个结构体即便内容完全一致,在C语言编译器看来是两种类型。
#include <stdio.h>
//结构体变量的定义
struct stu{
char name[20];
char sex[5];
int age;
char id[20];
}student;
struct stu{
char name[20];
char sex[5];
int age;
char id[20];
};
struct stu student;
//以上两种定义方式是相同的,将student定义为struct stu类型的变量
#include <stdio.h>
//初始化:定义变量的同时初始化
struct stu{
char name[20];
char sex[5];
int age;
char id[20];
}student2={ "haohao", "boy", 25, "012" };
struct stu student1 = { "renhao", "boy", 27, "717" };
//以上两种方式都可以
//结构体嵌套初始化
struct Number{
int data;
struct stu student3;
}node1 = { 1, { "guoren", "girl", 22, "0406" } };
struct Number node2 = { 2, { "xiaohao", "boy", 22, "0912" } };
- 结构体的自引用
(1)概念:结构体中包含一个成员为该结构体本身的成员
(2)必须定义为指针 ,struct 标签* 变量名
#include <stdio.h>
//结构体的自引用
struct Node{
int data;
struct Node* node1;//必须为指针,否则不知道其sizeof
};
typedef struct Node{
int data;
//_Node* node2;//此写法是错误的,因为不知道_Node是什么类型
struct Node* node2;
}_Node;
- 结构体成员访问
结构体变量访问成员是通过.操作符进行访问的;结构体指针访问指向变量的成员是通过->进行访问的
#include<stdio.h>
struct stu{
char name[20];
char sex[5];
int age;
char id[20];
};
int main()
{
struct stu student1;
struct stu* student2;
student1.age = 20;//使用.访问age成员
student2->age = 18;//使用->访问age成员,但变量必须为指针类型
system("pause");
return 0;
}
- 结构体传参
结构体传参时不传结构体,要传结构体的地址,因为结构体指针可能包含超大数组(结构体过大,参数在压栈的时候系统开销过大,导致性能下降)
#include <stdio.h>
//结构体传参时要传地址
struct stu{
char name[20];
char sex[5];
int age;
char id[20];
};
void Print_1(struct stu s)
{
printf("%d\n", s.age);
}
void Print(struct stu* s)
{
printf("%d\n", s->age);
}
int mian()
{
struct stu _stu = { "renhao", "boy", 27, "717" };
//传结构体
Print_1(_stu);
//传地址
Print(&_stu);
return 0;
}
- 结构体内存对齐
(1)本质:以空间换时间 ,提高效率

(2)结构体的对齐规则:
第一个成员在与结构体变量偏移量为0的地址处;
其他成员变量要对齐到某个数字的整数倍的地址处;
结构体总大小为最大对齐数(每个成员变量都有一个对齐数)的整数倍;
如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整数大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍;
结构体内包含数组只要第一个对齐。
(3)对齐:在放入特定变量时其实偏移量能整除对齐数
对齐数:变量自身大小(可以设置),VS中默认为8,Linux中默认为4
#include <stdio.h>
//结构体的内存对齐
struct s1{
char c1;//1
int i;//4
char c2;//1
// 1+3+4+1+3=12
};
struct s2{
double d;//8
char c;//1
int i;//4 8+1+3+4=16
};
//结构体嵌套问题
struct s3{
char c1;//1
struct s2 s;//16(s2的最大对齐数为8,则外部和其对齐)
double d;//8 1+7+16+8=32
};
struct s4{
char c1;//1
int a[3];//4 数组只用第一个对齐
double d;//8 1+3+4*3+8=24
};
int main()
{
printf("%d\n", sizeof(struct s1));
printf("%d\n", sizeof(struct s2));
printf("%d\n", sizeof(struct s3));
printf("%d\n", sizeof(struct s4));
system("pause");
return 0;
}
(4)为什么存在内存对齐?
平台原因:不是所有的硬件平台都能访问任意地址上的任意数据的;
性能原因:数据结构应该尽可能地在自然边界上对齐,原因在于,为了访问未对齐的内存,处理器需要做两次内存访问,而对齐内存访问仅需要一次访问。
(5)修改默认对齐数
#include <stdio.h>
//设置默认对齐数为8
#pragma pack(8)//
struct s1{
char c1;//1
int i;//4
char c2;//1
};
#pragma pack()//取消设置的默认对齐数,还原为默认
#pragma pack(1)
struct s2{
char c1;//1
int i;//4
char c2;//1
};
#pragma pack()
int main()
{
printf("%d\n", sizeof(struct s1));//12
printf("%d\n", sizeof(struct s2));//6
system("pause");
return 0;
}
- 结构体实现位段
(1)位段的成员必须是int、unsigned int、signed int、char;
(2)位段的成员名后边有一个冒号和一个数字;
(3)位段在空间上是按照需要以4个字节(int)或者1字节(char)的方式来开辟的;
(4)位段不是跨平台的,原因:
int位段被当成是有符号数还是无符号数是不确定的;
位段中最大的数目不确定(16位机器最大16,32位机器最大32,写成27在15位机器中会出现问题);
位段中的成员在内存中是从左向右分配,还是从右向左分配是不确定的;
当一个结构体包含两个位段时,第二个位段成员比较大,无法容纳第一个位段剩余的位时。是舍去剩余的位,还是利用是不确定的。
#include <stdio.h>
struct A
{
int _a : 2;//2是比特位
int _b : 5;
int _c : 10;
int _d : 30;
};
int main()
{
printf("%d\n", sizeof(struct A));//8 为字节数
system("pause");
return 0;
}
枚举
- 枚举类型的定义及使用
(1)枚举类型:把所有可能的值一一列举,{}中的内容是枚举类型可能取的值,也叫枚举常量(本质为整型)
(2)枚举常量都是有值的,默认从0开始,一次递增1,在定义时也可以赋初值。
#include <stdio.h>
//枚举类型的定义
enum Day{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
enum Sex{
Male,
Female,
secret
};
enum color{
red=1,
yellow=4,
blue//5
};
int main()
{
enum color clo = red;//只有枚举常量给枚举变量赋值,才不会出现类型的差异
printf("%d\n", clo);
system("pause");
return 0;
}
- 枚举的优点
(1)增加代码可读性和维护性;
(2)和#define定义的标识符相比枚举有类型检查,更加严谨;
(3)防止命名污染;
(4)便于调试;
(5)使用方便,一次可以定义多个常量
联合(共同体)
- 联合类型的定义
联合是一种特殊的自定义类型,这种类型定义的变量也包含一系列的成员,特征是这些成员共用同一块空间
#include <stdio.h>
union Um{
char c;
int i;
};
int main()
{
union Um un;//联合变量的定义
//计算联合变量的大小
printf("%d\n", sizeof(un));//4 char c 和int i共用同一个空间
system("pause");
return 0;
}
- 联合的特点
(1)联合的成员共用同一块内存空间,因此一个联合变量的大小,至少是最大成员的大小;联合体的任何一个成员都是联合体的第一个成员
(2)根据联合体的特定可以用来判断当前计算机的大小端存储
#include <stdio.h>
union Un{
int i;
char c;
};
int main()
{
union Un un;//联合变量的定义
//输出结果相同
printf("%d\n", &(un.i));
printf("%d\n", &(un.c));
//
un.i = 0x11223344;
un.c = 0x55;
printf("%x\n", un.i);//11223355
system("pause");
return 0;
}
- 联合大小的计算
(1)联合体的大小至少是最大成员的大小
(2)如果最大成员大小不是最大对齐数的整倍数的时候,就要对齐到最大对数的整倍数;联合体最后开辟的空间必须能够整除自身的最大对齐数
#include <stdio.h>
union un{
char ch[9];
int i;
};
int main()
{
printf("%d\n", sizeof(union un));//12
system("pause");
return 0;
}