c语言中如何计算偏移结构体中成员变量的偏移量

直接上代码

https://bbs.csdn.net/topics/340158940

参考问答

#include <stdio.h>
struct demo2{

};

struct demo{
    int b;
    int c;
    int a;
};

typedef struct
{
    unsigned long dwFreq;
    unsigned char afclocked;
    unsigned char ucSndsys;
}ProgInfo;
#define offsetof(type, field)  (long)&(((type*)0)->field)

int main() {
    printf("%d\n", offsetof(ProgInfo, ucSndsys));
    printf("%ld\n",(size_t)(&((struct demo*)0)->a));
    return 0;
}
发布了93 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_32783703/article/details/103736272
今日推荐