Abhishek wrote:
So, whats the difference between a null pointer and a pointer which is
pointing to void?
Are these same?
You require a proper explanation of what the terms 'null' and 'void'
actually mean in C. Their meanings differ from the standard English
sense (just to confuse everyone).
Null...
In the English language "null" means insignificant, missing or
amounting to nothing. It can also mean zero. In fact the German
language for number zero is "null".
http://german.about.com/library/anfang/blanfang07.htm
Don't let that confuse you.
In C terms a null pointer is a pointer that doesn't point to any
variable, structure or function *anywhere*. It's like having a finger
that is designed to point to a spot on a map. Point to anywhere on the
map and it will have some well defined coordinate (address), but if you
haven't decided where to point that finger quite yet... point that
finger upwards into space (null).
The null value for pointers is only useful for setting pointers that
haven't been initialised to a *real* address.
Null pointers *must* be assigned using the NULL macro (defined by
<stdio.h>). The reason is that, while most of the time NULL is defined
as 0, it may not be on every platform.
char *string = NULL; /* I'm not pointing to anything useful yet */
CALL FOR COMMENT HERE: I've never seen a compiler *not* use 0 as NULL,
so who has? Who can list an example where this is not the case? I've
seen so much code that relies on the NULL == 0 assumption.
Use of a null pointer?
Any pointer can be assigned the NULL value. I can be a char, int, long
or any structure or union pointer.
void PrintMyString(char *string)
{
if (string != NULL) /* Check if this is a real address of a
possible string */
printf("This is the string: %s", string);
}
and my favourite is:
void ProcessTaxReturn(struct TAX_RETURN *p_tax_return)
{
if (p_tax_return != NULL) /* Has a valid tax return structure been
received? */
{
SendLargeTaxBill(p_tax_return);
}
}
I've been trying for years to send a null tax return, but it's never
worked. :-)
Void...
In the English language "void" means empty, useless or a vacuum.
In C it has quite a different meaning. My best explanation is a type
of zero length data.
Consider:
void DoSomthing(void);
The compiler knows what there is no return and no arguments to this
function.
void*
What does this mean? It's a pointer like any other pointer. It holds
the address of a memory location.
It simply means that "I've got the address of *something*, but I don't
know what it is".
void *pointers are generally used between software components that
expose an interface where the type of data is unknown.
Consider
void *malloc(size_t size);
This standard C library function returns a void * as it has no idea how
this memory block is to be used for (something addressed in C++ with
the new operator).
void* pointers is a convenient way of passing around data pointers when
types con't be conveyed. Use them sparingly because real types (and
typedefs) should be used whenever possible.
I hope this goes some way to explaining how void and null should be
used.
Lucien Kennedy-Lamb