-1

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

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • `NULL` need not be 0 always. Check [this](https://stackoverflow.com/questions/21559293/what-type-is-null) post. – rinz1er Jun 27 '20 at 09:09
  • @ShriramRamesh But Null Pointers in C will always be 0 – thisisjaymehta Jun 27 '20 at 09:10
  • [Seriously, have any actual machines really used nonzero null pointers, or different representations for pointers to different types?](http://c-faq.com/null/machexamp.html) – David Ranieri Jun 27 '20 at 09:14
  • 2
    All `NULL` pointer variables will compare equal. – Weather Vane Jun 27 '20 at 09:16
  • 1
    @WeatherVane if they are of types such that implicit conversion exists (i.e. the same type, or one is `void *`). Trying `p == pp` would be a constraint violation – M.M Jun 27 '20 at 09:17
  • Does this answer your question? [What type is NULL?](https://stackoverflow.com/questions/21559293/what-type-is-null) – Roshin Raphel Jun 27 '20 at 09:18
  • @M.M I was quoting from C18 ***6.3.2.3 Pointers** 4 Conversion of a null pointer to another pointer type yields a null pointer of that type. **Any two null pointers shall compare equal**.* – Weather Vane Jun 27 '20 at 09:19
  • @WeatherVane perhaps you have discovered a defect in the Standard, as it is clearly ruled out by 6.5.9/2 – M.M Jun 27 '20 at 09:24
  • null pointers don't point anywhere: they're not doing what they are supposed to do (*like a compass needle pointing up*) – pmg Jun 27 '20 at 10:42

4 Answers4

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
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
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.
M.M
  • 138,810
  • 21
  • 208
  • 365
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).

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • So is it not guaranteed, that if we `memset` a pointer to zeros, its value will be the null pointer? [Example](https://godbolt.org/z/Hw99Es). – Dr. Gut Jun 27 '20 at 21:03
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.