我們在書寫C程序的時候,有時候需要根據(jù)結構體成員變量的地址,得到結構體的地址,特別是我們想用C來實現(xiàn)C++的繼承特性的時候。
我們對問題的分析如下:
- 輸入:一個結構體定義type,這個結構體中某個成員變量的名字member以及它的地址ptr
- 輸出:包含此成員變量的結構體的地址
為了便于分析,我們給出一個實例來說明
struct father_t { int a; char *b; double c; }f; char *ptr = &(f.b); //而不是 ptr = f.b; 這里ptr是b的地址,而不是它指向的地址。 |
根據(jù)C語言對struct類型的存儲特性,我們可以畫這么一個圖示:

通過分析圖示,我們可以看出,我們只需要把當前知道的成員變量的地址ptr,減去它在結構體當中相對偏移4就的到了結構體的地址(ptr-4)。
在linux當中對此有一個很好的宏可以使用,叫做 container_of, 放在 linux/kernel.h當中。它的定義如下所示:
/** * container_of - cast a member of a structure out to the containing structure * * @ptr: the pointer to the member. * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. * */ #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) |
對上面的定義,分析如下:
- (type *)0->member為設計一個type類型的結構體,起始地址為0,編譯器將結構體的起始的地址加上此結構體成員變量的偏移得到此結構體成員變 量的偏移地址,由于結構體起始地址為0,所以此結構體成員變量的偏移地址就等于其成員變量在結構體內的距離結構體開始部分的偏移量。即:&((type *)0->member)就是取出其成員變量的偏移地址。而其等于其在結構體內的偏移量:即為:(size_t)(& ((type *)0)->member)經(jīng)過size_t的強制類型轉換后,其數(shù)值為結構體內的偏移量。該偏移量這里由offsetof()求出。
- typeof(((type *)0)->member)為取出member成員的變量類型。用其定義__mptr指針。ptr為指向該成員變量的指針。__mptr為member數(shù)據(jù)類型的常量指針,其指向ptr所指向的變量處。
- (char*)__mptr轉換為字節(jié)型指針。(char*)__mptr - offsetof(type,member))用來求出結構體起始地址(為char *型指針),然后(type*)(char*)__mptr - offsetof(type,member))在(type *)作用下進行將字節(jié)型的結構體起始指針轉換為type *型的結構體起始指針。
這就是從結構體某成員變量指針來求出該結構體的首指針。指針類型從結構體某成員變量類型轉換為該結構體類型。