hi.. i have a doubt on what is a null pointer (is it a value or an address) and what does assigning null to a variable means ??..and how does it varies from a void pointer
hi.. i have a doubt on what is a null pointer (is it a value or an address) and what does assigning null to a variable means ??..and how does it varies from a void pointer
It might help to read this FAQ on null pointers.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The FAQ should answer your questions.
Null pointer is of pointer type, which is usually an address.
A null pointer can't point to any object or function -- a void pointer is a normal pointer that can point to any valid object/function, or null, but with void type - so no information on what the pointer points to. This means you can't dereference a void pointer -- you have to cast it to something first.
A common example of use of a void pointer is malloc. malloc returns a void*, because it can be cast to whatever you're allocating memory for.
A NULL pointer is a pointer who's value compares equal to zero, according to rules of the language. By convention, a pointer with value NULL (or zero) means it does not point at a valid object. Dereferencing a NULL pointer (i.e. accessing whatever the pointer points at) therefore yields undefined behaviour.
A void pointer is a completely different beast from a NULL pointer: a void pointer refers to a variable of type void *. Given only a void pointer, the compiler has no information about what type of object (int, float, a massive struct, etc) is pointed at, so attempting to dereference a void pointer yields a compilation error. However, if the programmer KNOWS the the type of object pointed at, the void pointer can be converted to a pointer to that type, and that new pointer can be dereferenced (although if the programmer gets the type conversion wrong, the behaviour is undefined). A void pointer (like a pointer to any type) can have a NULL value (indicating it does not point at any valid object).
Thank u........very heplful and that was very clear indeed....