1起始位置是数据类型的整数倍(我假设开始都从1开始,你也可以从0)
2结构体大小是结构体最大的数据类型整数倍
3嵌套的结构体起始位置是,嵌套结构体最大数据类型,整数倍
4有嵌套结构体先算嵌套结构体大小,然后把嵌套当作占据多少个空间的一个普通变量
#include <stdio.h>
#include<stdio.h>
struct S1
{
double a;
char q;
struct qw{
int y;
char u;
}s0;
int b;
char c;
}s1;
struct S2{
char ch;
char i;
struct s{
char ch1;
double j;
char ch2;
short e;
}w1;
char f;
double w;
}s2;
struct S3{
int x;
struct s{
char a;
int b;
short c;
double ch1;
int y;
}w2;
char f;
char b;
}s3;
int main()
{
// sizeof()功能:获取了数据在内存中所占用的存储空间,以字节为单位来计数。
printf("int=%d,short=%d",sizeof(int),sizeof(short));
printf("&s1.a=%d, &s1.s0.y=%d, &s1.s0.u=%d, &s1.b=%d, &s1.c=%d\n", &s1.a,&s1.s0.y,&s1.s0.u,&s1.b,&s1.c);
printf("s1大小为%d\n", sizeof(struct S1));
printf("&s2.ch=%d,&s2.i=%d, &s2.w1.ch1=%d,&s2.w1.j=%d,&s2.w1.ch2=%d,&s2.w1.e=%d,&s2.f=%d,&s2.w=%d\n",&s2.ch,&s2.i, &s2.w1.ch1, &s2.w1.j,&s2.w1.ch2,&s2.w1.e,&s2.f,&s2.w);
printf("&s2.w1.ch2=%d,&s2.w1.e=%d\n",&s2.w1.ch2,&s2.w1.e);
printf("struct S2大小为%d\n", sizeof(struct S2));
printf("&s3.x=%d &s3.w2.a=%d &s3.w2.b=%d &s3.w2.c=%d &s3.w2.ch1=%d &s3.w2.y=%d &s3.f=%d &s3.b=%d\n",&s3.x,&s3.w2.a,&s3.w2.b,&s3.w2.c,&s3.w2.ch1,&s3.w2.y,&s3.f,&s3.b);
printf("struct S3大小为%d\n", sizeof(struct S3));
return 0;
}