Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a null pointer and a void pointer?

Tags:

c

pointers

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

like image 415
Pavitar Avatar asked Aug 27 '10 05:08

Pavitar


People also ask

What is the difference between void and null?

The difference between null and void as term for nothing stems from their place in physical space. A void is nothing but takes up space; null is nothing at all. In other words, you could measure a void but null offers nothing to measure. 4.1 Void is used to indicate that a function/method does not return any data type.

What is a void pointer?

The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is the difference between a null pointer and a wild pointer?

Dangling (or wild) pointer: a pointer that points somewhere, but not to a valid object. Null pointer: a pointer that points to a specially designated out-of-bounds location that programs will never legally store data in.


2 Answers

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

like image 103
AnT Avatar answered Sep 20 '22 11:09

AnT


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.

like image 33
vanza Avatar answered Sep 17 '22 11:09

vanza