1.结构体字节对齐,64位没做完的做完,32位重新都做一遍,课上指定2字节对齐的做一遍,自己验证
64位 32位
#pragma pack(2) //指定二字节对齐
typedef struct
{
char c; //1 //1
struct A
{
short a; //2
int *b; //8
char c; //1
}p; //12
long b; //8
int a; //4
}T; //26
#pragma pack()
2.两种验证大小端对齐的代码写一遍
#include<stdio.h>
#include<string.h>
union A
{
char a;
int b;
};
int main(int argc, const char *argv[])
{
union A s;
s.b=0x12345678;
printf("%#X\n",s.a);
if(s.a==0x78)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main(int argc, const char *argv[])
{
int a=0x12345678;
char *p=&a;
if(*p==0x78)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
return 0;
}