1. 程式人生 > >如何取得結構體成員相對於結構體的偏移量

如何取得結構體成員相對於結構體的偏移量

舉個例子,有一個結構體

typedef struct {
char s;
int len;
}node;
我想知道,變數len相對於結構體的偏移量,怎麼辦?

奉上程式碼

#include<stdio.h>

typedef struct {
char s;
int len;
}node;
int main(){
int i=(int)&(((node*)0)->len);
printf("%d",i);
return 0;

}

結果:


nginx中的offset函式就是這麼幹的