是否始终保证局部变量的地址有效

Nik*_*ani 3 c pointers

我在编译器中遇到警告:"地址永远不会为NULL"
代码如下所示:

struct mydata * var = NULL;
/* some function which may modify var*/
if(NULL != &var) {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

实际警告(-Werror已标记):

error: the comparison will always evaluate as 'true' for the address of 'var' will never be NULL [-Werror=address]

那么这是否意味着局部变量的地址总是非NULL?比较它是一个错字&var.

Ste*_*and 8

如果您使用它,它将始终具有有效地址.

如果不使用它,编译器可能会对其进行优化或忽略.所以这个地址没有价值,但你永远不会知道.

  • 需要注意的是,"使用它"将包括获取变量的地址. (3认同)