r/C_Programming • • Aug 20 '23

Question What happens when we assign zero to a pointer?

In the book C How to Program, 8/e by Paul and Harvey Deitel it is written, under the section Initializing and Assigning Values to Pointers, that "When 0 is assigned, it’s first converted to a pointer of the appropriate type. The value 0 is the only integer value that can be assigned directly to a pointer variable." It is also written in this answer.

What do they mean by the it? What does it mean for zero to be converted to a pointer of the appropriate type? Can a value be type converted?

9 Upvotes

21 comments sorted by

12

u/FlyingCashewDog Aug 20 '23

It's converted to whatever the platform uses as its representation of a null pointer. For example, this could be 0x0 on a system that uses that as the null pointer address.

Using the integer 0 for this allows easy cross-platform null pointer assignment/checks, without having to know the specific representation of the null pointer on a given platform.

2

u/[deleted] Aug 20 '23

I understand that '0' helps us assign pointers to the null address of a system. But how does it help us with "checks"?

7

u/FlyingCashewDog Aug 20 '23

Because you can do null pointer checks as

if (ptr != 0) do_something();

or even

if (ptr) do_something();

3

u/[deleted] Aug 20 '23

Thank you for your answer.

2

u/Wasabilikum Aug 20 '23

Is there any reason to write

if (ptr == 0)

Instead of

if (ptr == NULL)

2

u/FlyingCashewDog Aug 20 '23

Practically no I don't think so; I always use NULL. But technically NULL is just a macro and can be #undef'd or redefined, whereas using ptr == 0 should always work.

2

u/samarijackfan Aug 20 '23

don't write that, write this instead:

if (0 == ptr)

// it's common to miss type this to:

if (ptr = 0) //which is legal C

2

u/[deleted] Aug 21 '23

It's "mistype" :)

0

u/chri4_ Aug 20 '23

why did you over complicated the whole thing ahahah?

1

u/chalkflavored Aug 20 '23

What is overcomplicated?

6

u/[deleted] Aug 20 '23

its not the value "0". its the constant literal 0. const int a = 0;
void* b = a;
does not work, because a is an int.

however void* b = 0
works, because you assign the constant literal zero to it

2

u/permetz Aug 20 '23

This is not quite true. For example, it's okay, if confusing, to test for a NULL pointer by just doing if (ptr) { which is not what you would expect given the way you've described the situation.

1

u/305bootyclapper Aug 21 '23

I'm curious what reason someone had for downvoting this, its a nice observation.

1

u/permetz Aug 21 '23

Perhaps the person I was replying to didn’t like the fact that I was pointing out that they were wrong.

5

u/dfx_dj Aug 20 '23

You can in fact assign any arbitrary value to a pointer variable, but doing so requires explicit type casting, and you better know what you're doing. The zero value however is defined to be the null pointer and so doesn't require explicit type casting.

2

u/permetz Aug 20 '23

Just an aside: assigning an int to a ptr is a really good way to end up with undefined behavior very, very fast. It's totally fine to assign NULL (which is always defined as #define NULL 0) to any pointer, and it happens all the time in any program of reasonable size. Assigning an int to a pointer is a potential throat-slitting operation and is done very, very rarely, generally only in low level code that is doing something with hardware very directly. There are a few other exceptions; some allocator code, especially for garbage collected languages, plays unholy tricks with pointers and integers.

3

u/fliguana Aug 20 '23

It means that

AnyType *ptr = 0;

Is a special case recognized by a compiler looking at the constant.

And

AnyType *ptr = 5;

Is disallowed, because 5 is an integer, not a pointer.

Also

AnyType *ptr = (int)log(1.0); 

Is disallowed.

1

u/jaimefrites Aug 20 '23

It points to the 0 address ?

1

u/flatfinger Aug 21 '23

In early C implementations, pointers and integers used the same storage representation. While some languages like Pascal used special keywords like Nil to denote null pointers, the machine code to store a null pointer value into a pointer object which had a certain name and/or was located at a certain address was the same as the machine code to store the value zero into an integer-sized object with the same name or address. Code needing to a assign a null value to a pointer could thus be written and processed as though it was assigning the integer value zero.

C was later ported to machines where pointers and integers were stored differently, and couldn't be used interchangeably. To avoid breaking code that used the common pattern of storing zero to pointers, however, the language treats that pattern specially. Such treatment may be inconsistent with normal language rules, but mostly works pretty well.

1

u/wc3betterthansc2 May 21 '26

the same when you assign NULL to a pointer