结构体和联合体
联合体是面试重点
struct student
{
char name[32];
int age;
char sex;
struct student …;
};
int main()
{
struct student s1;// 定义结构体变量
struct student s2 = {“aaa”,20,“m”};//初始化结构体变量
}
结构体变量要用printf逐个打印
虚拟内存
free(s3);//手动释放空间
重要问题 结构体占多少个字节
结构体数组
typedef struct student stu;
stu *s[10]
for ()
{
s[i] = (stu *)malloc(sizeof(stu));//申请空间
}
定义了指针数组首先要为其申请空间
否则只是定义的一个野指针,里面存储的是垃圾值。
联合体
特点:所有成员共享同一段内存空间
长度:最长成员的长度
即公用一段内存
作用:用于判断设备的大小端
高字节存放在高地址 表示小端
高字节存放在低地址 表示大端
union test
{
short value;
char ch[2];
}
char[0] 一定在低地址
char[1]一定在高地址
union test T
T.value = 0x0102 0000000100000010
if(T.ch[0] ==1 && T.ch[1]==2)
{
printf(高端字节序)
}
else if(T.ch[1] ==1 && T.ch[0]==2)
{
printf(低端字节序)
}
转换大小端
int main()
{
int a = 1;
printf("%d\n",((a&0x000000ff<<24) | ((a & 0x0000ff00)<< 8) | ((a&0x00ff0000)>>8 | ((a&0xff000000>>24))
return 0;
}
①与运算取值
②移位转换
③或运算求值
枚举类型
enum 内部元素自动递增作用