Possible Duplicate:
What's the difference between a null pointer and a void pointer?
What is the difference between a pointer to void and a NULL pointer in C? Or are they the same?
Possible Duplicate:
What's the difference between a null pointer and a void pointer?
What is the difference between a pointer to void and a NULL pointer in C? Or are they the same?
In C, there is void
, void
pointer and NULL
pointer.
void
is absence of type. I.E. a function returning a void
type is a function that returns nothing.void
pointer: is a pointer to a memory location whose type can be anything: a structure, an int, a float, you name it.NULL
pointer is a pointer to location 0x00
, 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.
Pointer to void is a pointer to an unspecified type. Ie. Just a pointer. It can still be a valid pointer, but we don't know what it points to (eg. A function might take a void pointer as a parameter, and then interpret the type according to a different parameter)
NULL is an 'empty' pointer. Not valid, can be used to specify a pointer to nothing / not set. It is a value whilst void is a type.