美文网首页
计算struct的成员的偏移量

计算struct的成员的偏移量

作者: CodingCode | 来源:发表于2022-03-31 08:14 被阅读0次
#include <stdio.h>
#include <string.h>

struct st1_t {
    int a;
    int b;
};

struct st_t {
    int magic;
    short connect;
    char master[3];
    struct st1_t st1;
};

main() {
    printf("%d\n", &(((struct st_t *)0)->magic));
    printf("%d\n", &(((struct st_t *)0)->connect));
    printf("%d\n", &(((struct st_t *)0)->master));
    printf("%d\n", &(((struct st_t *)0)->st1));
    printf("%d\n", &(((struct st_t *)0)->st1.a));
    printf("%d\n", &(((struct st_t *)0)->st1.b));
}

结果打印成员member相对于st_t的偏移量:

0
4
6
12
12
16

相关文章

  • 计算struct的成员的偏移量

    结果打印成员member相对于st_t的偏移量:

  • 预处理,const,sizeof

    1.宏定义 用宏定义去求一个结构体struct里某个变量相对struct的偏移量。要求偏移量首先struct a;...

  • 侵入式链表实现

    先来看2个宏 offset 宏的目的是推算出一个成员的偏移量。 (struct_type*)0 将0x0 强制转为...

  • C语言结构体大小的计算方式

    定义 结构体中的偏移量 结构体中的偏移量是一个成员的实际地址和结构体首地址之间的距离。 结构体大小计算 结构体大小...

  • C语言结构体大小计算

    定义 结构体中的偏移量 结构体中的偏移量是一个成员的实际地址和结构体首地址之间的距离。 结构体大小计算 结构体大小...

  • struct 的匿名成员

    package main import"fmt" type Animal interface{ eat() } t...

  • 30小时快速精通C++

    struct和class的区别 1、struct的默认成员权限是public2、class的默认成员权限是priv...

  • 结构体内存对齐

    想要计算结构体大小,必须先掌握结构体内存对齐规则: 1.第一个成员在与结构体变量偏移量为0的地址处。2.其他成员变...

  • C++知识点(自用)

    关于struct和class c++中的struct可以有成员函数,能继承,能实现多态。。。那struct和cla...

  • class and struct

    相同点: struct能包含成员函数, struct能继承, struct能实现多态 不同点: 默认的继承访问权...

网友评论

      本文标题:计算struct的成员的偏移量

      本文链接:https://www.haomeiwen.com/subject/uipgjrtx.html