Null pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.
Void pointer is a specific pointer type - void * - a pointer that points to some data location in storage, which doesn't have any specific type.
So, once again, null pointer is a value, while void pointer is a type. These concepts are totally different and non-comparable. That essentially means that your question, as stated, is not exactly valid. It is like asking, for example, "What is the difference between a triangle and a car?".
Answer from AnT stands with Russia on Stack OverflowNull pointer is a special reserved value of a pointer. A pointer of any type has such a reserved value. Formally, each specific pointer type (int *, char * etc.) has its own dedicated null-pointer value. Conceptually, when a pointer has that null value it is not pointing anywhere.
Void pointer is a specific pointer type - void * - a pointer that points to some data location in storage, which doesn't have any specific type.
So, once again, null pointer is a value, while void pointer is a type. These concepts are totally different and non-comparable. That essentially means that your question, as stated, is not exactly valid. It is like asking, for example, "What is the difference between a triangle and a car?".
They are two different concepts: "void pointer" is a type (void *). "null pointer" is a pointer that has a value of zero (NULL). Example:
void *pointer = NULL;
That's a NULL void pointer.
The two concepts are orthogonal:
- A void pointer, (
void *) is a raw pointer to some memory location. - A null pointer is a special pointer that doesn't point to anything, by definition. It can be a pointer to any type, void or otherwise.
A void pointer can be null or not:
void *void_ptr1 = nullptr;
void *void_ptr2 = malloc(42);
void *void_ptr3 = new Foo; // void * can point to almost anything
void *void_ptr4 = (char*)void_ptr3 + 1; // even somewhere inside an object
A non-void pointer can also be null or not:
Foo *f = nullptr;
Foo *g = new Foo;
Just plain forget about that answer. A quote from your link :
"a pointer with no return type is called a null pointer."
This is sooo plain WRONG. A pointer's return type? REALLY? This is a bad source...
void* is universal pointer type because any pointer type (except for pointer to const and/or volatile) can be implicitly converted to void*. In other words, you can assign any pointer to a variable of type void*. A null pointer is a pointer value 0
In C, there is void, void pointer and NULL pointer.
voidis absence of type. I.E. a function returning avoidtype is a function that returns nothing.voidpointer: is a pointer to a memory location whose type can be anything: a structure, an int, a float, you name it.- A
NULLpointer is a pointer to location0x00, that is, no location. Pointing to nothing.
Examples:
void function:
void printHello()
{
printf("Hello");
}
void pointer:
void *malloc(size_t si)
{
// malloc is a function that could return a pointer to anything
}
NULL pointer:
char *s = NULL;
// s pointer points to nowhere (nothing)
void is a datatype. void* is just a pointer to an undefined type. A void* can be set to any memory location. A NULL pointer is a any pointer which is set to NULL (0).
So yes, they are different, because a void pointer is a datatype, and a NULL pointer refers to any pointer which is set to NULL.
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 );
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.