• 周四. 5月 30th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

结构体元素偏移量宏的定义及解析

admin

11月 28, 2021

如何通过编码实现结构体元素的地址偏移量:

通过网上搜索到一个通过宏定义的方法实现,大致内容如下:

#define offset(struct_name, struct_member) (unsigned int)(&(((struct_name *)0)->struct_member))

通过实验验证,该宏能够正确实现结构体元素的偏移量的计算;

结构体元素的偏移量是针对首地址的,因此,第一步就是确定当前结构体的地址

(struct_name *)0是常数0强制转换为struct_name类型的指针的类型转换符,这样该指针就指向了结构体的首地址,
然后通过该指针指向不同的成员,并取得不同成员的地址进行显示转换,最终得到各个成员的地址偏移量。

《结构体元素偏移量宏的定义及解析》有4个想法
  1. Thanks a bunch for sharing this with all of us you really know what you’re
    speaking approximately! Bookmarked. Kindly also discuss with my website =).
    We can have a hyperlink change agreement among us I saw similar here:
    E-commerce

  2. You’re so awesome! I don’t believe I’ve read something like
    this before. So great to find another person with some original thoughts on this subject.
    Really.. many thanks for starting this up. This site is something that is needed on the
    web, someone with a little originality! I saw similar here:
    Najlepszy sklep

  3. Hey there! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my blog to rank for some targeted
    keywords but I’m not seeing very good results. If you know of any please share.
    Cheers! You can read similar article here: Najlepszy sklep

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注