1. 结构体结构体类型的声明
1.1 结构的基础知识
1.2 结构的声明
struct tag
{
member-list;
}variable-list;
struct Stu
{
char name[20];//名字
int age;//年龄
char sex[5];//性别
char id[20];//学号
}; //分号不能丢
struct stu
{
//属性
char name[20];
int age[10];
}s1,s2;
1.3 匿名结构体类型
//匿名结构体类型
struct
{
//属性
char name[20];
int age[10];
}s1;
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
struct
{
char name[20];
int age[20];
}x;
struct
{
char name[20];
int age[20];
}s1[20], * p;
int main()
{
p = &x;
return 0;
}
- 结构的自引用
struct Node
{
int data;
struct Node* next;
};
- 结构体变量的定义和初始化
struct Point
{
int x;
int y;
}p1; //声明类型的同时定义变量p1
struct Pointp2;
//定义结构体变量p2
struct Stu //类型声明
{
char name[15];//名字
int age; //年龄
};
struct Stus = {"zhangsan", 20};//初始化
struct Node
{
int data;
struct Pointp;
struct Node* next;
}n1= {10, {4,5}, NULL}; //结构体嵌套初始化
struct Node2= {20, {5, 6}, NULL};//结构体嵌套初始化
- 结构体内存对齐
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
struct s1
{
char c1;
char c2;
int i;
};
struct s2
{
char c1;
int i;
char c2;
};
int main()
{
printf("%d\n", sizeof(struct s1));
printf("%d\n", sizeof(struct s2));
return 0;
}
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<stddef.h>
struct s1
{
char c1;
int i;
char c2;
};
int main()
{
printf("%d\n",offsetof(struct s1,c1));
printf("%d\n",offsetof(struct s1, i));
printf("%d\n",offsetof(struct s1, c2));
return 0;
}
struct S
{
char c;
int i;
};
struct S s;
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
//#pragma pack(4)
struct s1
{
int c1;
double c2;
};
int main()
{
printf("%d\n", sizeof(struct s1));
return 0;
}
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
pragma pack(4)
struct s1
{
int c1;
double c2;
};
int main()
{
printf("%d\n", sizeof(struct s1));
return 0;
}
- 结构体传参
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); //传地址
return 0;
}
- 结构体实现位段
6.1 什么是位段
struct A
{
//4byte=32bit
int_a:2;
int_b:5;
int_c:10;
//余15
4byte=32bit
int_d:30;
};
printf("%d\n", sizeof(struct A));
6.2 位段的内存分配
struct S
{
char a : 3;
char b : 4;
char c : 5;
char d : 4;
};
int main()
{
struct S s = { 0 };
printf("%d\n", sizeof(struct S ));
s.a = 10;
s.b = 12;
s.c = 3;
s.d = 4;
return 0;
}
6.3 位段的跨平台问题
6.4 位段的应用
7.枚举
7.1 枚举类型的定义
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
enum Day//星期
{
Mon = 1,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
int main()
{
printf("%d\n", Mon);
printf("%d\n", Tues);
printf("%d\n", Wed);
printf("%d\n", Thur);
printf("%d\n", Fri);
printf("%d\n", Sat);
printf("%d\n", Sun);
}
7.2 枚举的优点
7.3 枚举的使用
enum Color
//颜色
{
RED=1,
GREEN=2,
BLUE=4
};
enum Color clr = GREEN;
//只能拿枚举常量给枚举变量赋值,才不会出现类型的差异。
clr = 5;
- 联合(共用体)
8.1 联合类型的定义
union Un
{
char c;
int i;
};
union Un un;
int main()
{
printf("%d\n", sizeof(un));
printf("%d\n", &(un));
printf("%d\n", &(un.c));
printf("%d\n", &(un.i));
}
8.2 联合的特点
union Un
{
char c;
int i;
};
union Un un;
int main()
{
printf("%d\n", &(un.c));
printf("%d\n", &(un.i));
un.i = 0x11223344;
un.c = 0x55;
printf("%x\n", un.i);
}
int check_sys()
{
int a = 1;
return *(char*)&a;
}
int main()
{
int ret = check_sys();
if (ret == 1)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
}
int check_sys()
{
union Un
{
char c;
int i;
}u;
u.i = 1;
return u.c;
}
int main()
{
int ret = check_sys();
if (ret == 1)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
}
8.3 联合大小的计算
union Un1
{
char c[5];
int i;
};
union Un2
{
short c[7];
int i; };
int main()
{
//下面输出的结果是什么?
printf("%d\n", sizeof(union Un1));
printf("%d\n", sizeof(union Un2));
return 0;
}