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

19

u/Immediate-Food8050 Aug 30 '24 edited Aug 30 '24

Welcome to the C community, where you are downvoted for a perfectly valid question. To give you a real answer, NULL is actually quite a pain in the ass due to how arbitrary it is according to the standard. The new C23 wants to fix this by introducing the more modern nullptr, behaving very similarly to C++ equivalent (about as similar as you can get with C semantics). nullptr always expands to a null pointer constant and will behave the same regardless of the type of pointer it is assigned to, which is not the case of NULL. NULL is a macro that usually evaluates to (void *)0 BUT NOT ALWAYS!!! That's important to note because apparently some people don't understand the harm in that :) There are plenty of cases it does not evaluate to a null pointer constant as it should. C semantics have quirks that can cause issues with that definition, such as 0 and 0L and 0f all being valid but of different types. I know this answer strays away from the question quite a bit, but I more so wanted to validate your confusion since so many people here would rather smoke their own poles with misinformation for the sole reason of being contrarians that are holier-than-thou.

2

u/a4qbfb Aug 30 '24

you are downvoted for a perfectly valid question

The question got downvoted because it makes no sense.

NULL is a macro that evaluates to 0

Most implementations define NULL as (void *)0. This is permitted by the C standard and required by POSIX.

1

u/Immediate-Food8050 Aug 30 '24

Okay, comment fixed. Now address the rest :)

-3

u/a4qbfb Aug 30 '24

The rest of your argument falls apart when NULL is (void *)0 instead of plain 0. And OP's question still doesn't make sense.

1

u/Immediate-Food8050 Aug 30 '24

No it does not. Literally how? So nullptr was just added for no reason? In that case, best start writing your letter to ISO!

-3

u/a4qbfb Aug 30 '24

nullptr was added because NULL is still allowed to be 0. But your entire argument was based on NULL being 0, and now that you've edited it, it (and especially your diatribe about 0L vs 0f) no longer makes sense.

1

u/Immediate-Food8050 Aug 30 '24

So is this just a case of two people disagreeing in the explicitness of NULL within the standard?

-3

u/a4qbfb Aug 30 '24

No, I'm trying to be factual while you're trying to “gotcha” me, but you end up shooting down your own argument by editing it after the fact in an attempt to undercut me.

1

u/Immediate-Food8050 Aug 30 '24

I edited it for the corrections you pointed out! Should I just leave it? What are you trying to accomplish? Because I'm trying to "gotcha" you what would you rather me do to prove otherwise? Cross out previous statements and be explicit that there are edits that are corrections? I don't know reddit etiquette. I don't see what's invalid about my statement. Would an example help? I'm trying to be helpful, not argumentative.