Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we call (void *)0 a void pointer in C?

Summarizing the C standard, specifically ISO/IEC 9899:201x §6.3.2.3 - 3:

If a pointer is being compared to the constant literal 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The C standard defines that 0 cast to the type void * is both a null pointer and a null pointer constant.

Now if we look at (void *)0 it is an address(illegal in this context) that is pointing to void.

Normally we can cast such an address to any appropriate pointer datatype and dereference it but here even after casting it to some other pointer type it is illegal to dereference it.

So my doubt is:

Can we call (void *)0 as a void pointer looking the way it is defined?

Also see the below code:

void *pointer = NULL;

What will I call it now? A void pointer, a null pointer or a null void pointer?

like image 946
LocalHost Avatar asked Dec 18 '22 12:12

LocalHost


2 Answers

What will i call it now ? A void pointer, a null pointer or a null void pointer ?

In this declaration

void *pointer = NULL;

there is declared a pointer of the type void * that is a null pointer due to initializing it with a null pointer constant.

A pointer to object of any type can be a null pointer.

The casting of the zero integer constant to void * is used because a pointer of the type void * can be implicitly converted to any other object pointer type. Another advantage of using the type void * is that you may not dereference a pointer of that type because the type void is always an incomplete type.

Early versions of C did not have the type void. Instead of the type void there was used the type char. So for example in old C programs you can encounter something like the following

memcpy( ( char * )p1, ( char * )p2, n );
like image 68
Vlad from Moscow Avatar answered Dec 30 '22 21:12

Vlad from Moscow


So my doubt is can we call (void *)0 as a void pointer looking the way it is defined ?

It is certainly a void pointer. The declaration void *pointer means that you declare pointer as a pointer to void. pointer will never be anything else than a void pointer. It's type will never change, but the value can change from null to something else. Also, the expression (void *)x basically mean "cast x to a void pointer`. So this expression also has the type pointer to void.

What will i call it now ? A void pointer, a null pointer or a null void pointer?

I would never use the phrase "null void pointer". A null pointer is a pointer of any type, pointing at null. A void pointer is simply a pointer pointing to void. Combining them to one phrase would only call confusion. It is both a void pointer and a null pointer. Any pointer can be a null pointer. Consider this:

int *p = (void*)0;

p is a null pointer, but it's not a void pointer.

like image 39
klutt Avatar answered Dec 30 '22 20:12

klutt