r/C_Programming • • Aug 30 '24

Question Why do we use Null pointers?

If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?

0 Upvotes

56 comments sorted by

View all comments

72

u/zzmgck Aug 30 '24

The value where a null pointer is pointing is irrelevant. A null pointer is a pointer that is pointing nowhere--there is no value.

In C, the number 0 was defined as the representation of the null pointer. They could have picked any number, but it would have been painful.

7

u/Cylian91460 Aug 30 '24

Its hardware depends iirc, that means null can be something else then 0, it can be max pointer value for example

2

u/TheSkiGeek Aug 30 '24

Integer value 0 casts to/from (and compares equal to) a “null pointer”. But yes, it’s not guaranteed that a null pointer has the same bit pattern as integer zero on any particular architecture. It’s just some pointer value that’s guaranteed to never be used as a legitimate pointer.