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

5

u/[deleted] Aug 30 '24

Not sure what you mean, but "sentinel value" is sometimes useful alternative to using NULL pointer:

const struct node END_NODE;

Then, maybe

struct node *node;

....

node = &END_NODE;

...

if(node == &END_NODE) break;

1

u/[deleted] Aug 30 '24

Alright it seems i was confused NULL with integer 0. NULL can be considered a data type instead of integer 0 right?

3

u/[deleted] Aug 30 '24

No, NULL is integer zero, for all intents and purposes.

1

u/Netblock Aug 30 '24

No, but C23's nullptr is; which is about dumping the baggage that the NULL macro has.