char、int、double、float基本类型
结构体的大小必须可以整除最宽基本成员
结构体成员的地址-结构体首地址=偏移量,偏移量必须是当前成员整数倍
结构体尾部不足的部分,就会被填充
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4,只有一个元素,不存在对齐。
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//4
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4
int b;//4
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//8
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4
double b;//8
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//16
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4
double b;//8
int c;//4
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
double b;//8
char c;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char c;//1
double b;//8
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//16
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char c;//1
char b;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//3
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char c;//1
short b;//2
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//4
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
int b;//4
double c;//8
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
int b;//4
int c;//4
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//16
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
short b;//2
int c;//4
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//12
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
double b;//8
int c;//4
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char b;//1
int c;//4
char d;//1
double e;//8
};
int main()
{
struct MyStruct my1;
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
double b;//8
int c;//4
char d;//1
};
int main()
{
struct MyStruct my1;
printf("%d\n", sizeof(struct MyStruct));//24
printf("%p\n\n", &my1);
printf("%p\n", &my1.a);//取出结构体成员的地址
printf("%p\n", &my1.b);//a的地址+8
printf("%p\n", &my1.c);//b的地址+8
printf("%p\n", &my1.d);//c的地址+4
system("pause");
return 0;
}
结构体的大小必须可以整除最宽基本成员
结构体成员的地址-结构体首地址=偏移量,偏移量必须是当前成员整数倍
结构体尾部不足的部分,就会被填充
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4,只有一个元素,不存在对齐。
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//4
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4
int b;//4
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//8
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4
double b;//8
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//16
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
int a;//4
double b;//8
int c;//4
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
double b;//8
char c;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char c;//1
double b;//8
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//16
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char c;//1
char b;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//3
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char c;//1
short b;//2
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//4
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
int b;//4
double c;//8
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
int b;//4
int c;//4
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//16
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
short b;//2
int c;//4
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//12
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
double b;//8
int c;//4
char d;//1
};
int main()
{
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
char b;//1
int c;//4
char d;//1
double e;//8
};
int main()
{
struct MyStruct my1;
printf("%d\n", sizeof(struct MyStruct));//24
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct MyStruct
{
char a;//1
double b;//8
int c;//4
char d;//1
};
int main()
{
struct MyStruct my1;
printf("%d\n", sizeof(struct MyStruct));//24
printf("%p\n\n", &my1);
printf("%p\n", &my1.a);//取出结构体成员的地址
printf("%p\n", &my1.b);//a的地址+8
printf("%p\n", &my1.c);//b的地址+8
printf("%p\n", &my1.d);//c的地址+4
system("pause");
return 0;
}