且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

铸造NULL在C一个struct指针?

更新时间:2023-12-01 08:38:16

在分配例子明确投不作任何有用的意义。然而,似乎你的问题实际上是关于的#define NIL((名单*)NULL)宏,它的实用性超出分配。

In assignment example the explicit cast make no useful sense. However, it seems that you the question is really about #define NIL ((List *) NULL) macro, whose usability extends beyond assignment.

一个地方,它可能是有意义的是,当你把它传递给一个可变参数函数,或者没有原型声明的函数。该标准 NULL 可以被定义为 0 0L ((无效*)0)或以其它方式,这意味着它可以是除$ p $在这种类型的无上下文PTED不同。显式类型转换将确保它是正确PTED为指针间$ P $。

One place where it might make sense is when you pass it to a variadic function or to a function declared without a prototype. The standard NULL can be defined as0 or 0L or ((void *) 0) or in some other way, meaning that it might be interpreted differently in such type-less contexts. An explicit cast will make sure that it is interpreted correctly as a pointer.

例如,这通常是无效的(行为是未定义)

For example, this is generally invalid (behavior is undefined)

void foo();

int main() {
  foo(NULL);
}

void foo(List *p) {
  /* whatever */
}

在更换与呼叫

foo(NIL);

使得它有效。