Whats the difference between a Null pointer & a Void pointer?
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.
Void refers to the type. Basically the type of data that it points to is unknown.
Null refers to the value. It's essentially a pointer to nothing, and is invalid to use.
A null pointer points has the value NULL
which is typically 0, but in any case a memory location which is invalid to dereference. A void pointer points at data of type void. The word "void" is not an indication that the data referenced by the pointer is invalid or that the pointer has been nullified.
A null pointer is guaranteed to not compare equal to a pointer to any object. It's actual value is system dependent and may vary depending on the type. To get a null int
pointer you would do
int* p = 0;
A null pointer will be returned by malloc
on failure.
We can test if a pointer is null, i.e. if malloc
or some other function failed simply by testing its boolean value:
if (p) {
/* Pointer is not null */
} else {
/* Pointer is null */
}
A void pointer can point to any type and it is up to you to handle how much memory the referenced objects consume for the purpose of dereferencing and pointer arithmetic.
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?".
Usually a null pointer (which can be of any type, including a void pointer !) points to:
the address 0, against which most CPU instructions sets can do a very fast compare-and-branch (to check for uninitialized or invalid pointers, for instance) with optimal code size/performance for the ISA.
an address that's illegal for user code to access (such as 0x00000000 in many cases), so that if a code actually tries to access data at or near this address, the OS or debugger can easily stop or trap a program with this bug.
A void pointer is usually a method of cheating or turning-off compiler type checking, for instance if you want to return a pointer to one type, or an unknown type, to use as another type. For instance malloc() returns a void pointer to a type-less chunk of memory, the type of which you can cast to later use as a pointer to bytes, short ints, double floats, typePotato's, or whatever.
NULL
is a value that is valid for any pointer type. It represents the absence of a value.
A void pointer is a type. Any pointer type is convertible to a void pointer hence it can point to any value. This makes it good for general storage but bad for use. By itself it cannot be used to access a value. The program must have extra context to understand the type of value the void pointer refers to before it can access the value.