typeof operator

http://gcc.gnu.org/onlinedocs/gcc/Typeof.html

 

typeof is a way to refer to the type of an expression, supported by gcc
e.g.
#define max(a, b)/
({typeof(a) _a = (a);/
typeof(b) )_b =(b);/
_a>_b?_a:_b;})

To declare y as an array of the same type as x:
typeof (x) y[4];
又如: #define pointer(T) typeof(T*)

 


e.g. to cast a member of a structure to the container structure

#define container_of(ptr, type, member) ({ /
             const typeof(((type *)0)->member)) *__mptr = (ptr); /
            (type *)( (char *)__mptr - offsetof(type, member));  })


#define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章