结构体字节对齐

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;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值