相关知识点如图所示:
结构体重点知识点总结
1、结构体的特殊声明:
struct //匿名结构体
{
int a;
int b;
float c;
}x;
struct
{
int a;
int b;
float c;
}a[20], *p;
int main() {
p = &x; //警告,两个声明的类型不同,所以是非法的
2、结构体的不完整声明:
struct B;//缺省后,编译器智能时可以编译成功
struct A
{
int a;
int b;
struct B;
};
struct B
{
int a;
int b;
struct A;
};
3、结构体成员访问:
struct A
{
int a;
int b;
struct B;
}x;
int main() {
struct A x;
x.a = 20; //变量名加.访问变量
4、结构体的自引用:
typedef struct A
{
int a;
int b;
struct A* c;
}x;
5、结构体的内存对齐:
struct s2
{
char a;//1
int b;//4
};//4 --8
struct s4{
char a;//1+7
struct s3 {
double d;//8
char b[3];//1->3+1
int i;//4 ->16
};//8->16+1+7
char j[3];//1->24+3+1
char *c[2];//4->28+8+4
double e;//8->48
struct s2 f[2];//4->48+16
char g;//1->65
};//65 不能整除结构体S4中最大对齐数,结果为72
int main() {
printf("%d\n",sizeof(struct s4));//72
位段(比特位)
struct A
{
int a : 2;
int b : 5;
int c : 6;
};//位段大小为1
枚举的使用
enum color
{
RED=100,
GREEN,
blue,
YELLOW
}c;
int main(){
enum color c;
c= RED ;
printf("%d %d %d\n", RED,GREEN,YELLOW);//100 101 103
联合与共同体的巧妙使用
简单实现整型转换成字符串
union ip_addr
{
unsigned long addr;
struct
{
unsigned char c1;
unsigned char c2;
unsigned char c3;
unsigned char c4;
}ip;
};
int main() {
union ip_addr my_ip;
my_ip.addr = 176238749;
printf("%d %d %d %d\n", my_ip.ip.c4, my_ip.ip.c3, my_ip.ip.c2, my_ip.ip.c1 );
//10 129 48 157
system("pause");
}