判断结构体变量所占字节数

规则

1,结构体总长度一定是最长字节数的整数倍,double除外,若出现double还是除它之外的最大类型的字节数的整数倍,double还是按8字节算

2,每个成员偏移量一定是改成员长度的整数倍

eg


#include <stdio.h>

struct student
{
    char name[20];
    int a;
    short b;
};

int main()
{
    printf("%d\n", sizeof(struct student));
    return 0;
}

 

嵌套结构体所占字节数示例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct AA
{
    int a;
    char b;
};
struct student
{
    short c;
    struct AA aa;

    char sex;
    
};

int main()
{
    printf("%d\n", sizeof(struct student));
 
    return 0;
}

说明:

1,struct AA aa,占8个字节; 然后将其整体带入struct student{....}结构体中, short 补两个字节 4+8+1=13, 需要再补三个字节使其为4的倍数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值