container_of 宏不适用于 char* 或数组成员

container_of macro does not work for char* or array members

我正在阅读 John Madieu 的 Linux 设备驱动程序开发,其中一段说

The container_of macro won't work for char * or array members. It
means the first member of container_of must not be a pointer to
another pointer to char nor to array in the structure.

这是container_of的定义:

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

所以如果我有

struct person {
int age;
int salary;
char *name;
} me;

我有 char ** my_name = &(me.name);,为什么我不能执行以下操作:

struct person * me = container_of(my_name,struct person,name);

这是由于 ISO C 关于指针初始化的规则,在这种情况下会破坏 __mptr 的初始化。

这是一个简单的例子:

int main()
{
    char ar[5] = {0};
    const char (*ptr)[5] = &ar;
}

// warning: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic]

(live demo)

. Note that C++ has no such limitation 上有关于这个问题的讨论; const可以自由添加。

A kernel dev discussion 建议将 __mptr 替换为另一种在 container_of 中执行类型检查的方法,这样您可能会发现这已经不再影响您了。