tags:

views:

248

answers:

7

Whats the difference between a Null pointer & a Void pointer?

+1  A: 

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.

vanza
A: 

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.

Shawn D.
+1  A: 

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.

Jesse Dhillon
`NULL` in C is either `0` or `(void *)0`, and in C++ `NULL` is always `0`. The implementation's internal representation for a null pointer might not be 0 (as in all bits cleared), however.
dreamlax
+3  A: 

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.

aaronasterling
+10  A: 

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?".

AndreyT
@AndreyTIs Null Pointer of any use,as in when we write codes?
Pavitar
@Pavitar, It's useful as a marker. For example, to indicate a pointer that has not been allocated memory or to mark the end(s) of a linked list.
Vulcan Eager
You can and you should use Null Pointers. For example, If malloc can't allocate memory, it returns a null pointer. It is a good practice to put your pointers to null after free() 'em to avoid Dangling Pointers http://en.wikipedia.org/wiki/Dangling_pointer
Marco
@Pavitar: The null pointer constant is guaranteed to be invalid, and it's identifiable. You can't confuse it with any valid pointer. Therefore, it can be used as a signal of failure, or a marker that there's no more data, or that a pointer doesn't point anywhere, or anything like that.
David Thornley
@David Thornley -Thankyou.
Pavitar
A: 

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.

hotpaw2
void *foo = 0L; // is both a void pointer and a null pointer
hotpaw2
Whether the null pointer points to address 0 is completely irrelevant to its proper use, although it's nice if it does so if you're debugging and examining variable values. Nor is it necessary to cause any sort of fault, although I think that's a good feature. It's guaranteed to compare equal to any other null pointer value of the same type, and guaranteed to compare unequal to any valid pointer, and that's the real use.
David Thornley
A: 

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.

JaredPar