欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c 语言 offsetof 函数

程序员文章站 2024-03-17 16:57:04
...

该宏返回值的类型是size_t,该类型成员的偏移值。

例子:

#include <stddef.h>
#include <stdio.h>

struct address {
   char name[50];
   char street[50];
   int phone;
};

int main()
{
   printf("name offset = %d byte in address structure.
",
   offsetof(struct address, name));

   printf("street offset = %d byte in address structure.
",
   offsetof(struct address, street));

   printf("phone offset = %d byte in address structure.
",
   offsetof(struct address, phone));

   return(0);
} 

输出:

name offset = 0 byte in address structure.
street offset = 50 byte in address structure.
phone offset = 100 byte in address structure.