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:
void *
) is a raw pointer to some memory location.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;