logologo

C 结构体对齐

Dec 17, 2023

结构体的大小必须是其最大成员的整数倍

struct student_type1 {
    double score;//double是一种浮点类型,8个字节,浮点分为float和double
    short age;//short 是整型,占2个字节
};

struct student_type2 {
    double score;
    int height;//如果两个小存储之和是小于最大长度8,那么它们就结合在一起
    short age;
};

struct student_type3 {
    int height;
    char sex;
    short age;
};

结构体的大小必须是其最大成员的整数倍

注意

定义student_type3时,由于 char 占 1 字节,short 占 2 字节,两个小存储之和是小于最大长度 8,那么它们就结合在一起; 内存地址如下:

注意注意注意

浙ICP备2021022773号    2022-PRESENT © ZhengKe