0
点赞
收藏
分享

微信扫一扫

C 求结构体的字节数及各元素偏移量

芷兮离离 2022-03-20 阅读 49
c++
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<stddef.h> //offsetof头文件


struct my_struct
{
    char a[11]; //11+5
    double b;   //8
    int c;      //4+4
    double d;   //8
    int e;      //4
    char f;     //1+3
};


void test01()
{
    struct my_struct p = { 'a',4,3,12,10,'b'};

    printf("d的偏移量为:%d\n", offsetof(struct my_struct, d));
    printf("该结构体的字节数:%d\n",sizeof(p));
}

int main()
{
    test01();

    system("pause");
    return EXIT_SUCCESS;
}

具体规则可参考https://blog.csdn.net/motadou/article/details/3521399

举报

相关推荐

0 条评论