Is a valid local variable address always guaranteed

In my compiler I came across a warning: "the address will never be NULL"
The code looks like this:

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

      

Actual warning (-Werror marked):

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

Does this mean that the address of a local variable is always non-NULL? It was a typo to compare with &var

.

+3


source to share


2 answers


If you use it yes, it will always have a valid address.



If you don't use it, it will probably be optimized or ignored by the compiler. Thus, this address will not make any difference, but you will never know it.

+8


source


C 2011 Section 6.3.2.3 Clause 3

An integer constant expression with a value, 0

or an expression that is applied to a type , is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unevenly with a pointer to any object or function. void *

C 2011 Footnote 66

Macro is NULL

defined in <stddef.h>

(and other headers) as a null pointer constant



Therefore, a null pointer never points to a valid object.

+1


source







All Articles