So I was going through some interview questions and I came across one about void and null pointers, which claims:
a pointer with no return type is called a null pointer. It may be any kind of datatype.
This confused me thoroughly! It seems void and null could be used interchangeably according to this question, and I don't believe that to be correct. I assumed void to be a return type and null to be a value. But I am just a code-rookie and am not sure I am right.
Please express your views as to what a null pointer is and a void pointer is. I am not looking for difference between null and void.
The two concepts are orthogonal:
- A void pointer, (
void *
) is a raw pointer to some memory location.
- A null pointer is a special pointer that doesn't point to anything, by definition. It can be a pointer to any type, void or otherwise.
A void pointer can be null or not:
void *void_ptr1 = nullptr;
void *void_ptr2 = malloc(42);
void *void_ptr3 = new Foo; // void * can point to almost anything
void *void_ptr4 = (char*)void_ptr3 + 1; // even somewhere inside an object
A non-void pointer can also be null or not:
Foo *f = nullptr;
Foo *g = new Foo;
Just plain forget about that answer. A quote from your link :
"a pointer with no return type is
called a null pointer."
This is sooo plain WRONG. A pointer's return type? REALLY? This is a bad source...
void*
is universal pointer type because any pointer type (except for pointer to const and/or volatile) can be implicitly converted to void*
. In other words, you can assign any pointer to a variable of type void*
. A null pointer is a pointer value 0
The void
type in general means that no type information is given.
You should always keep in mind that a pointer conveys two pieces of information: the type of the pointed data (int
, double
, ...), which specifies how to interpret it, and the address of the data it points to, which specifies where you can get the actual value of the pointed data.
The type information is in the type of the pointer (double
*
, int
*
, ...), while the address of the data is the actual value contained in the pointer variable.
So, a void
pointer (void *
) is a pointer that do not specify any type information. It tells you where the data is, but it doesn't tell you how to interpret it. You know that at that address there's something, but you don't know if it's an int
, a double
or an array of flying cows. To actually use such data, you have to get type information about it in some other way (e.g. with some other magic parameter), cast that pointer to a regular pointer type and then use it as usual.
void *
is often used in C to provide some kind of support to generic programming; see for example the qsort
C library function.
A NULL
pointer, instead, is a pointer that points to nothing. In this case, the type information about the pointer in general is present, but it's the address of the pointed data that is missing. Of course, it's possible to have a void *
that is NULL
.
Quick example (supposing that v
is declared as double v;
):
Type information present
+----------------------+----------------------+
| ✔ | ✘ |
+---+----------------------+----------------------+
p c | | | |
v o o | ✔ | double * ptr = &v; | void * ptr = &v; |
a i n | | | |
l n t +---+----------------------+----------------------+
i t e | | | |
d e n | ✘ | double * ptr = NULL; | void * ptr = NULL; |
d t | | | |
+---+----------------------+----------------------+
Trivia: NULL
, at least in the current standard, is guaranteed to be 0.
In other areas of the language, void
is always used to specify lack of type. Using it as return value (note: I'm talking now about void
, not void *
) means that the function does not return any value, and casting an expression to void is a fancy way to discard a value (you're signaling to the compiler and to other programmers that you're conscious that you're not using a certain value).
Please, tell us: whats the difference:
- between gas tank and no-gas situation
- between cookie jar and no-cookies
- between term 'money' and 'empty pockets'
If you come up with these, you'l be able to grasp null vs void* dillema.
void is a non-type.
null is a non-value.
Here's some differences with respect to pointer arithmetic:
It stems from the fact that void is an incomplete type.
void *vp;
vp++; // error, incomplete type
vp += 2; // same error
void *p = 0;
p++; // still same error
int *p = 0;
p++; // well-formed program, but UB ($5.6/5)
The linked article is simply wrong. Its first sentence:
a pointer with no return type is
called a null pointer
is triggering all sorts of alarms for me. This is a highly confused piece of writing.
You are almost correct. "Pointer to void" is a type (not a "return type"). Values of any type can be returned by functions, and thus be (the function's) return type.
A null pointer is a pointer that, regardless of its type, is pointing at the null object, which is not any valid object that can be created. A null pointer can be said to point at "nothing".
A pointer to void can also be null;
void *nothing = 0;
is perfectly valid code, and just says that this pointer is capable of pointing a an untyped object, but right now it isn't.
null pointer is point to 0x000000(which is incorrect to access pointer), while void pointer is a correct pointer to an unspecified type(void *
). However, void pointer can be null pointer, but then unreferencing the pointer will generate error.
A void *ptr
is the pointer which can be used to point any type of data. It maybe int
, float
, double
. It has no return type that is initially pointer is created with pointer type (having hex value) and we can assign this pointer to any type of data.
While null pointer is the pointer with having NULL value as address, the pointer is assigned NULL value so that it cannot be used to access others data which its address may contain while creation. I think it is good programming technique to assign a pointer NULL if its not used at the moment.