- 结构体(复合类型)
将多个相同或不同类型的元素存放在一块连续的内存中,称为结构体
结构体的定义和初始化
//定义结构体关键字:struct 结构体类型为:struct Student 结构体的名字:Student
//定义结构体时 {}后需要加分号;结构体的成员包含在{}中
//结构体 atruct Student 是一个类型,没有空间,不能直接在结构体里给结构体成员赋值
struct Student
{
int id;
int age;
char name[128];
}a; //定义类型时,同时定义了一个结构体变量struct Student a
int main()
{
struct Student stu1 = {1,24,"ubuntu"}; //定义结构体变量并初始化,此时系统会分配空间
struct Student stu2 = { .age = 24 }; //给部分成员初始化,其他成员内容为0
struct Student stu3;
//通过结构体变量操作结构体成员,使用 成员运算符. 初始化结构体
stu3.id = 3;
stu3.age = 25;
//stu3.name = "hello";//err 数组名是一个常量,不能被赋值
strcpy(stu3.name, "hello");// ok
//通过结构体的地址(指针)操作结构体成员,使用 “->” 初始化结构体
struct Student *stu4;
stu4->id = 4;
stu4->age = 24;
strcpy(stu4->name, "hello");
system("pause");
return 0;
}
- 结构体数组
数组中每一个元素都是一个结构体
struct Student
{
int id;
int age;
char name[128];
};
int main()
{
//定义一个结构体数组,结构体数组有5个元素,每个元素都是struct Student类型
struct Student num[5] = { {1,23,"abs"},{ 2,4,"fvhdf" },{ 3,21,"dfghhj" },{ 4,23,"asdfbs" },{ 5,19,"sdfabs" } };
for (int i = 0; i < sizeof(num) / sizeof(num[0]); i++)
{
printf("%d %d %s", num[i].id, num[i].age, num[i].name);
}
system("pause");
return 0;
}
- 结构体对齐方式及大小
对齐方式:要判断一个结构体所占的空间大小,大体来说分三步走:
1.先确定实际对齐单位,其由以下三个因素决定
(1) CPU周期
WIN vs qt 默认8字节对齐
Linux 32位 默认4字节对齐,Linux 64位默认8字节对齐
(2) 结构体最大成员(基本数据类型变量)
(3) 预编译指令#pragma pack(n)手动设置 n–只能填1 2 4 8 16
上面三者取最小的,就是实际对齐单位(这里的“实际对齐单位”是我为了方便区分随便取的概念)
2.除结构体的第一个成员外,其他所有的成员的地址相对于结构体地址(即它首个成员的地址)的偏移量必须为实际对齐单位或自身大小的整数倍(取两者中小的那个)
3.结构体的整体大小必须为实际对齐单位的整数倍。
结构体大小只要记住以下三点即可:
1.结构体每个成员变量的相对首地址必须是该变量长度的整数倍;
2.结构体总大小必须为最大成员长度的整数倍;
3.union长度为最大成员变量的长度;
- 结构体套结构体
struct Student
{
int id;
int age;
char name[128];
};
struct MyStudent
{
struct Student s;//嵌套结构体 Student
int grades;
char sex[10];
};
int main()
{
MyStudent a;
a.grades = 99;
strcpy_s(a.sex, "male");
a.s.age = 10;
a.s.id = 23;
strcpy_s(a.s.name, "ubuntu");
printf("%d %d %s %d %s", a.s.id, a.s.age, a.s.name,a.grades,a.sex);
system("pause");
return 0;
}
- 结构体赋值
相同类型的变量可以相互赋值
struct Student
{
int id;
int age;
char name[128];
};
void memcpy_str(struct Student *x, struct Student *y)
{
memcpy(x, y, sizeof(x));
}
int main()
{
struct Student stu1 = { 1,24,"ubuntu" };
struct Student stu2;
//将stu1的值赋给stu2
//1、使用内存拷贝
memcpy_str(&stu2, &stu1);//1、使用内存拷贝
//2、每个成员分别拷贝
stu2.id = stu1.id;
stu2.age = stu1.age;
stu2.id = stu1.id;
strcpy(stu2.name, stu1.name);
//3、直接赋值
stu2 = stu1;
system("pause");
return 0;
}
- 结构体指针
定义结构体指针时,需要给指针申请空间,否则会造成空指针。
struct Student
{
int id;
int age;
char name[128];
};
int main()
{
//struct Student *p;//err p是一个野指针
struct Student *p = (struct Student *)malloc(sizeof(struct Student));//给p申请空间
p->id = 100;
p->age = 30;
strcpy_s(p->name, "ubuntu");
printf("%d %d %s", p->id, p->age, p->name);
free(p);
system("pause");
return 0;
}
- 嵌套结构体指针
注意:需要注意避免使用野指针
struct t
{
int a;
};
struct tea
{
int id;
char *p;
struct t *b;
};
int main()
{
struct tea *tmp = (struct tea *)malloc(sizeof(struct tea));
tmp->id = 100;
tmp->p = (char *)malloc(100);//注意,这里的P是一个指针,必须先申请空间,要不然就会造成野指针
strcpy_s(tmp->p, "hello");
//tmp->b->a = 298;//err 此时 b 是野指针,不能之恶杰赋值 b 所指向的空间
tmp->b = (struct t *)malloc(sizeof(struct t));
tmp->b->a = 123;
//释放空间的时候,需要里到外,层层释放
free(tmp->p);
free(tmp->b);
free(tmp);
system("pause");
return 0;
}
- 结构体数组作为函数形参
结构体数组作为函数形参 时 会退化为指针
struct Student
{
int id;
int age;
char name[128];
};
void set_num(struct Student *p, int n)
{
for (int i = 0; i < n; i++)
{
// p[i].id = i <===> (p+i)->id <===> (*(p + i)).id
(*(p + i)).age = i + 10;
(*(p + i)).id = i;
}
}
int main()
{
struct Student num[5];
memset(num, 0, sizeof(num));
//结构体数组作为函数形参
set_num(num, sizeof(num) / sizeof(num[0]));
//打印
for (int i = 0; i < sizeof(num) / sizeof(num[0]); i++)
{
printf("%d %d %s\n", num[i].age, num[i].id, num[i].name);
}
system("pause");
return 0;
}
- const 修饰结构体指针
同指针用法一致
struct Student
{
int id;
int age;
char name[128];
};
int main()
{
struct Student a;
struct Student b;
struct Student const *p1 = &a; //const修饰的是指针
//p->id = 100;// err 指针p1被const修饰,不能通过指针p1修改P1所指向的空间的内容
struct Student * const p2 = &a; //const修饰的是变量p2,p2指向结构体a
struct Student * const p2 = &b; // err , 变量p2被const修饰,不能修改p2的内容,即不能让p2指向另一个结构体
system("pause");
return 0;
}
- 联合体(共用体)
联合体Union是一个能在同一个存储空间内存储不同数据的类型;
多个变量共用同一个空间,联合体变量的地址和它的各个成员的地址是同一地址 但每一瞬间只能有一个成员使用;
联合体所占内存大小等于其最大成员所占空间的倍数;
联合体中起作用的成员是最后一次存放的成员,再存入一个新的成员时,原有的成员的值会被覆盖
union MyUnion
{
char a;
short b;
int c;
};
struct MyStruct
{
char a;
short b;
int c;
};
int main()
{
union MyUnion uniontmp;
uniontmp.a = 0x01;
uniontmp.c = 0x01020304;
uniontmp.b = 0x0a0b;
printf("%x\n", uniontmp.b); //0a0b
printf("%x\n", uniontmp.a); //01
printf("%x\n", uniontmp.c); //01020a0b
struct MyStruct structtmp;
printf("%d\n", sizeof(uniontmp)); // 大小为4字节
printf("%d\n", sizeof(structtmp)); //大小为8字节
system("pause");
return 0;
}
- 大小端存储&union验证
小端存:低位存低地址,高位存高地址;
大端存:低位存高地址,高位存低地址
int a = 0x01020304;
//小端存 --> 01020304
printf("%x\n", a);
//大端存 --> 04030201
大端存储:大型网络服务器,网络上的数据包等
小端存储:小型计算机,个人pc等
union验证大小端存储
union MyUnion
{
short b;
char buf[2];
};
int main()
{
union MyUnion tmp;
tmp.b = 0X0102;
if (tmp.buf[0] == 0x01)
{
printf("大端存储");
}
else
{
printf("小端存储");
}
system("pause");
return 0;
}
- 枚举类型
枚举:将变量的值一个个列举出来,变量的值只限于列举出来的范围内。
枚举变量的值只能赋值为{ } 里面的值,{ }里面的值是常量
枚举{ }里列举的常量的值默认从0 开始,但可以重新赋值。
//枚举类型默认从 0 开始
enum
{
A,//0
B,//1
C,//2
D=5,//可以给enum成员重新赋值,赋值之后,以此为基准,再往后排
E,//6
F,//7
G//8
}MyEnum; //这是枚举类型的名称
- typedef 用法
typedef 用于给类型取别名
用法:typedef 原类型 新类型名
#define CHAR char *
typedef char* CHAR32;//tyoedef 后加分号
int main()
{
CHAR x, y;//想要 char *x, char *y
printf("%d %d \n", sizeof(x), sizeof(y)); //sizeof(x) 为4字节,表示 x 是 char * 类型,但是sizeof(y) 为1字节,表示 y 是 char 类型
CHAR32 j, k;
printf("%d %d \n", sizeof(j), sizeof(k));//sizeof(j)、sizeof(k) 都为4字节,表示 j,k 都是 char * 类型
system("pause");
return 0;
}