一.struct结构体(封装数据、存放多种不同的数据类型)
struct的声明放在全局区
1. 声明和定义
声明:
struct student
{
int id;
char name[20];
int age;
};
struct student stu_array[3] =
{
{1, "zhangsan", 12},
{2, "lisi", 13},
{3, "wangwu", 15}
};
int i;
for(i = 0; i < 3; i++)
{
scanf("%d", &(stu_array[i].id));
scanf("%s", stu_array[i].name);
scanf("%d", &(stu_array[i].age));
}
for(i = 0; i < 3; i++)
{
printf("%d\t", stu_array[i].id);
printf("%s\t", stu_array[i].name);
printf("%d\t", stu_array[i].age);
printf("\n");
}
#include
struct student
{
char ch;
char ch1;
int num;
int num1;
};
int main()
{
struct student stu;
printf("sizeof(stu) = %d\n", sizeof(stu));
}
sizeof(stu) = 12;//字对齐
struct student
{
char ch;
int num;
char ch1;
int num1;
};
sizeof(stu) = 16;//字对齐,内存空洞
struct student
{
char ch;
short num;
char ch1;
char ch2;
};
sizeof(stu) = 6;//半字对齐
struct student
{
int num[3];//12
char ch[5];//8
char *ptr;//4
double b;//8
struct student *next;//4
};
sizeof(stu) = 36;
定义:
struct student stu;
2. 初始化(结构体变量用“.”访问,结构体指针用“->”访问)
1) struct student stu = {12, "zhangsan",17};
2) struct student stu =
{
.id = 12,
.name = "zhangsan",
.age = 17
};
3) struct student
{
intid;
char*name;
intage;
};
stu.name = (char *)malloc(sizeof(char) * 100);
scanf("%s", stu.name);
//structstudent *p_stu = &stu;//p_stu == &stu;*p_stu== stu;
//scanf("%s", p_stu.name);
4) stu.id = 1;
(*p_stu).id = 1;
p_stu->id = 1;
(&stu)->id = 1;
scanf("%d", &stu.id);
scanf("%d", &(p_stu.id));
5) stu.name = “zhangsan”;//此种写法错误,数组名为指针常量,不可以直接使用。
strcpy(stu.name, “zhangsan”);
strcpy(p_stu->name, “zhangsan”);
3. 结构体数组
struct student stu_array[3] =
{
{1, "zhangsan", 12},
{2, "lisi", 13},
{3, "wangwu", 15}
};
int i;
for(i = 0; i < 3; i++)
{
scanf("%d", &(stu_array[i].id));
scanf("%s", stu_array[i].name);
scanf("%d", &(stu_array[i].age));
}
for(i = 0; i < 3; i++)
{
printf("%d\t", stu_array[i].id);
printf("%s\t", stu_array[i].name);
printf("%d\t", stu_array[i].age);
printf("\n");
}
4. 使用注意事项
声明struct结构体时,相同数据类型放在一起,防止内存空洞。
实例:
#include
struct student
{
char ch;
char ch1;
int num;
int num1;
};
int main()
{
struct student stu;
printf("sizeof(stu) = %d\n", sizeof(stu));
}
sizeof(stu) = 12;//字对齐
struct student
{
char ch;
int num;
char ch1;
int num1;
};
sizeof(stu) = 16;//字对齐,内存空洞
struct student
{
char ch;
short num;
char ch1;
char ch2;
};
sizeof(stu) = 6;//半字对齐
struct student
{
int num[3];//12
char ch[5];//8
char *ptr;//4
double b;//8
struct student *next;//4
};
sizeof(stu) = 36;
5. 作用
利用结构体封装返回多个值
封装函数的形参
二.union共用体(和struct类似)
1. 声明
union node
{
intnum;
charch;
};
2. 定义
union node p;
union node * pp = &p;
3. 初始化
p.num = 1;
pp->num = 1;
p.ch = ‘a’;
4. 注意事项
共用体与结构体的区别在于:共用体是共用一块内存空间,会发生值覆盖的情况。
例如:
p.num = 1;
p.ch = ‘a’;
printf(“%d\n”, p.num);
此时输出的值为97,发生了值覆盖。
5. 大端字节序&小端字节序(CPU的属性)
小端CPU存放方式:低字节放在低地址对应空间,高字节放在高地址对应空间
大端CPU存放方式:低字节放在高地址对应空间,高字节放在低地址对应空间
(无论大端还是小端都是从低地址开始存放)
判断CPU是小端字节序还是大端字节序的方法
1).共用体
#include
union node
{
intnum;
charch;
};
int main()
{
intnum = 0x12345678;
char*p = #
if(*p== 0x78)
{
printf("small!\n");
}
else
{
printf("big!\n");
}
return 0;
}
2).指针
#include
union node
{
intnum;
charch;
};
int main()
{
unionnode p;
p.num = 0x12345678;
if(p.ch== 0x78)
{
printf("small!\n");
}
else
{
printf("big!\n");
}
return0;
}#include
enum node
{
A,//#define A 0
B = 5,//#define B 5
C,//#defien C 6
D = 7,//#define D 7
E,//#defien E 8
};
int main()
{
enumnode p;
printf("p.A= %d\n", A);
printf("p.B= %d\n", B);
printf("p.C= %d\n", C);
printf("p.D= %d\n", D);
printf("p.E= %d\n", E);
return0;
}
三.enum枚举(相当于整型)
#include
union node
{
intnum;
charch;
};
int main()
{
unionnode p;
p.num = 0x12345678;
if(p.ch== 0x78)
{
printf("small!\n");
}
else
{
printf("big!\n");
}
return0;
}#include
enum node
{
A,//#define A 0
B = 5,//#define B 5
C,//#defien C 6
D = 7,//#define D 7
E,//#defien E 8
};
int main()
{
enumnode p;
printf("p.A= %d\n", A);
printf("p.B= %d\n", B);
printf("p.C= %d\n", C);
printf("p.D= %d\n", D);
printf("p.E= %d\n", E);
return0;
}
枚举相当于整数宏,提高代码可读性(杜绝幻数)