标签:float img style ber print short oat roc turn
offsetof和container_of宏
offsetof宏的作用:计算结构体中某个元素相对结构体首地址的偏移量
container_of宏的作用:知道结构体变量中某个成员的指针,反推这个结构体变量的指针
#include <stdio.h> #define offsetof(type,member) ((int) &((type *)0)->member) #define container_of(ptr,type,member) ({ const typeof(((type *)0)->member) *mptr = (ptr); (type *)((char *)mptr - offsetof(type,member));}) //typeof关键字的作用:typeof(a)由变量a获得变量a的类型 typedef struct node{ int a; double b; float c; } n1; int main() { n1 s; float *p = &(s.c); printf("offsetof(c) = %d\n",offsetof(n1,c)); printf("addr of s is %p\n",&s); printf("container_of(p,n1,c) = %p\n",container_of(p,n1,c)); return 0; }
sizeof(short) = 2 sizeof(int) = 4 sizeof(long) = 4 -------------------------------- Process exited after 0.1803 seconds with return value 0 请按任意键继续. . .
标签:float img style ber print short oat roc turn
原文地址:https://www.cnblogs.com/embeded-linux/p/11616418.html