-1

For example int *p = NULL; and int **pp = NULL;, p and pp all point to the address 0?

9

4 Answers 4

2

After type adjustments (appropriate casts or casts to some void pointer) to satisfy constraints they'll compare equal to each other and equal to the null pointer constant (i.e., 0, some other integer constant expression equal to 0 or the same cast to (void*)0).

Whether those differently typed null pointers will have the same representation and whether that representation is all-bits zero is technically unspecified.

IOW, the following will always hold:

p == (void*)pp && p == 0 && pp == 0 //TRUE 

but this might not (although it does on most platforms):

(uintptr_t)p == 0 && (uintptr_t)pp == 0 && 0==memcmp(&p, (char[sizeof(p)]){0}, sizeof(p)) && 0==memcmp(&pp, (char[sizeof(pp)]){0}, sizeof(pp)) //COULD BE FALSE 
Sign up to request clarification or add additional context in comments.

1 Comment

Integer constant expression
0

Let’s start with the definition of the NULL pointer.

6.3.2.3 Pointers
...
3     An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

4     Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.
66) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.19.
C 2011 Online Draft

So, a null pointer does not necessarily mean address 0, it just means “whatever value the underlying system uses to indicate an invalid address” - think of it as a well-defined “nowhere”. It could be 0x00000000, it could be 0xFFFFFFFF, it could be 0xDEADBEEF, or anything else.

As far as your source code is concerned, NULL is always zero-valued, so you can test for NULL pointers like

int *ptr = NULL; ... if ( ptr ) // ptr is not NULL, do something with it else // ptr is NULL 

Two null pointers will evaluate to the same value, modulo any differences in type representation (different pointer types may have different size and alignment requirements).

1 Comment

So is it not guaranteed, that if we memset a pointer to zeros, its value will be the null pointer? Example.
0

Null pointers do not point anywhere. That's the whole reason for a null pointer to exist.

Generally speaking, pointers fall into one of the following disjoint categories:

  • Points to an object (including one-past-the-end).
  • Indeterminate (this includes dangling and uninitialized pointers).
  • Null.

Comments

0

Semantically null pointers do not point to memory. Even if you phrased the question a "If they could be considered pointing to memory, would they always point to the same memory" - the answer would be no.

In x86 real mode programs it was often case that the data segment was separate from the code segment, and then it might be possible that no object pointer value could even possibly point to a function, and no function pointer value could point to an object, and even though the null pointer values would have been represented as 0 and considered pointing to some memory address, they would have been different memory locations for object and function pointers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.