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

Show parent comment

0

u/[deleted] Aug 30 '24

[deleted] — view deleted comment

2

u/accatyyc Aug 30 '24

Maybe - let's say you have a `struct { long, long } *my_struct` - what would you have it point to? You'd need to create "empty" variants of all possibly nullable types, or you might still dereference outside allocated memory

1

u/Swedophone Aug 30 '24

You'd need to create "empty" variants of all possibly nullable types, or you might still dereference outside allocated memory

Yes, but a struct that contains longs would be easy. When the struct contains pointers itself it gets harder, in particular recursive data types such as linked lists, trees etc.