[Solved] NULL as a pointer valid address? [closed]


NULL as a pointer valid address?

Maybe: NULL is a macro which expands to a null pointer constant. It may have a value like int 0. In that case, it is not a pointer but a int. It may have a value like ((void*)0) which is a pointer. When a null pointer constant is converted to void *, it is a null pointer. No object will have an address that is equal to a null pointer. The null pointer can be assigned to any pointer variable.

Can we assign or compare a valid pointer with 0?

Yes. any_type *ptr = 0; is valid. The comparison if (ptr == 0) is valid. The int 0 is converted to a null pointer as part of the assignment / compare.

where could we assign NULL as legal?

any_type *ptr = NULL; is a legal assignment.

See what I know NULL is a invalid pointer , that pointing to heap memory , which pointer has not freed yet. But is the address of the pointer is valid?

The address is or is not valid is irrelevant. Pointing to heap memory or not is irrelevant. In C, assigning a null pointer to a pointer variable is valid. Comparing a null pointer to a valid object is a valid comparison and the result never matches.

Dereferencing a pointer that has the value of a null pointer is undefined behavior (UB). It may “work”, it may crash the code. It is UB.

solved NULL as a pointer valid address? [closed]