46

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Shouvik
  • 11,350
  • 16
  • 58
  • 89

9 Answers9

54

The two concepts are orthogonal:

  1. A void pointer, (void *) is a raw pointer to some memory location.
  2. 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;
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • could you please give an example? Are you saying like to a pointer to a void function or something? I am a newbie, so I would catch on better if you could elaborate bit :) – Shouvik Dec 02 '10 at 12:16
  • 14
    The type `void *` simply means "a pointer into memory; I don't know what sort of data is there". Any "normal" pointer (`int *`, `char *`, `MyStruct *`) is implicitly castable to `void *`; it's the archetypal "just a bit of memory". – Chowlett Dec 02 '10 at 12:19
  • @Chowlett: Yes, I was a bit incautious in my description. Amended. – Marcelo Cantos Dec 02 '10 at 12:23
32

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

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Thanks I guessed so much... Sticking to it now just for the questions... :) – Shouvik Dec 02 '10 at 12:17
  • 1
    Actually, a null pointer might not be of value '0', even though I've never seen any other value for it. On some weird platforms, 0 might be a valid pointer value. – Gianni Dec 02 '10 at 12:22
  • 1
    @Gianni yes you are right, and I am glad this info can stick with my answer in the form of your comment – Armen Tsirunyan Dec 02 '10 at 12:23
  • that does not explain the difference which the OP is looking for – Chubsdad Dec 02 '10 at 12:28
  • @Gianni: yes and know. Though the underlying representation (bytes) of a null pointer is implementation dependent, `0` represents a null pointer in the C++ language. It is up to the compiler to generate the appropriate code when `0` is to be treated as a pointer and produce the correct underlying representation depending on the implementation. – Matthieu M. Dec 02 '10 at 13:19
  • @Matthieu Right. But the point was about *value* '0'. Its ambiguous. I actually don't like this part of the C++ standard anyway. This should be scrapped in favour of nullptr from now on. But we are only splitting hairs here. Fact is, *void* is type, *NULL* is value. – Gianni Dec 02 '10 at 13:29
  • @Gianni: I also prefer `nullptr`, but I am stuck with gcc3.4 for the foreseeable future :) I upvoted Armen for his answer anyway. – Matthieu M. Dec 02 '10 at 13:51
28

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).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
12

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.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
  • 1
    I presume the confusion was genuine coz of the question... SO was supposed to be source of all knowledge coding related hence I though it would be an appropriate question. sorry if you feel otherwise... – Shouvik Dec 02 '10 at 12:22
  • @Shouvik: What do you mean? This is exactly the best answer to your question! Literally :) – Armen Tsirunyan Dec 02 '10 at 12:24
  • 1
    ummm the reason like I mentioned is I am not a seasoned programmer and am now interested in more than just being able to put a piece of program together but actually understand that language and its finer aspects. I see the jest though in the lack of comparison between the two... :) – Shouvik Dec 02 '10 at 12:27
  • lol, I get it.. :D Maybe if you put **no-gas-tank**, **no cookie jar** and **no empty pockets** I would have grasped it faster... :) – Shouvik Dec 02 '10 at 12:35
9

void is a non-type. null is a non-value.

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
2

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.

Tomasz Pluskiewicz
  • 3,622
  • 1
  • 19
  • 42
2

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

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)
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • So basically a void pointer is no good but it is a valid piece of code yeah? – Shouvik Dec 02 '10 at 12:30
  • 1
    @Shouvik: No No. It is very good sometimes, e.g when you just want to pass it just as a opaque Handle or something and not disclose the actual type to which it points, or e.g. if you don't want the called entity to do any pointer arithmetic. So it depends – Chubsdad Dec 02 '10 at 12:34
  • Aah okay.. Like in closures and stuff? – Shouvik Dec 02 '10 at 12:36
  • @Chubsdad: hi, why is your last examlpe with int ptr UB? can you please say? –  Jul 13 '14 at 17:53
1

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.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • "Incorrect" is a rather fuzzy adjective for pointers. `NULL` is a valid value for any pointer, but there's no object at pointer value NULL. Also, by your logic, a `void*` pointer cannot be NULL as it would be both "correct" and "incorrect". That's patently untrue. – MSalters Dec 02 '10 at 13:11