Why this 0 in ((type*)0)->member in C?

前端 未结 2 659
独厮守ぢ
独厮守ぢ 2020-12-14 17:31

The container_of() macro in the Linux kernel is defined as:

#define container_of(ptr, type, member) ({ \\
        const typeof( ((type*)0)->member) * __mp         


        
2条回答
  •  Happy的楠姐
    2020-12-14 18:14

    Because type* is a type and not a valid instance of the structure.

    The pointer to zero is used to get a proper instance, but as typeof is resolved at compile-time and not at run-time the address used in the pointer doesn't have to be a proper or valid address.

提交回复
热议问题