contaier_of中的第三个参数是第二个参数的成员!那第一个参数ptr指的是什么意思?解决办法

contaier_of中的第三个参数是第二个参数的成员!那第一个参数ptr指的是什么意思?
各位,请帮我解释一下container_of 中三个参数的具体函意,尤其是第一个参数!谢谢!

------解决方案--------------------
linux?
------解决方案--------------------
第一个参数的是成员的地址,第二个是结构体类型 第三个是成员名!
------解决方案--------------------
网上 google 了一下,还真的有:

http://blog.csdn.net/yinkaizhong/article/details/4093795

问题:如何通过结构中的某个变量获取结构本身的指针???

关于container_of见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) );})
container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.

关于offsetof见stddef.h中:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
TYPE是某struct的类型 0是一个假想TYPE类型struct,MEMBER是该struct中的一个成员. 由于该struct的基地址为0, MEMBER的地址就是该成员相对与struct头地址的偏移量.
关于typeof,这是gcc的C语言扩展保留字,用于声明变量类型.
const typeof( ((type *)0->member ) *__mptr = (ptr);意思是声明一个与member同一个类型的指针常量 *__mptr,并初始化为ptr.
(type *)( (char *)__mptr - offsetof(type,member) );意思是__mptr的地址减去member在该struct中的偏移量得到的地址, 再转换成type型指针. 该指针就是member的入口地址了.


例一;

container_of宏定义在[include/linux/kernel.h]中:

#define container_of(ptr, type, member)     /

const typeof( ((type *)0)->member ) *__mptr = (ptr); /

(type *)( (char *)__mptr - offsetof(type,member) );

offsetof宏定义在[include/linux/stddef.h]中:

#define offsetof(type, member) ((size_t) &((type *)0)->member)
下面用一个测试程序test.c来说明

#include<stdio.h>
struct student{
    char name[20];  
    char sex;
}stu={"zhangsan",'m'};

main()
{
    struct student *stu_ptr;    //存储container_of宏的返回值
    int offset;            //存储offsetof宏的返回值
//下面三行代码等同于 container_of(&stu.sex,struct student, sex )参数带入的情形

    const typeof(((struct student*)0)->sex) *_mptr = &stu.sex;
//首先定义一个 _mptr指针, 类型为struct student结构体中sex成员的类型
//typeof 为获取(((struct student*)0)->sex)的类型,此处此类型为char
//((struct student*)0)在offsetof处讲解

    offset = (int)(&((struct student *)0)->sex);
/*((struct student*)0)为 把 0地址 强制转化为指向student结构体类型的指针
该指针从地址 0 开始的 21个字节用来存放name 与 sex(char name〔20〕与 char sex共21字节)