Void means that the function does not have to return a value. Sometimes it wouldn't be useful or meaningful to return a value like int, double, char, or any other valid type. Usually you use void if you do not need a return value, so it's useful when working with pointers.

For example, let's say you want to make a program that sorts array elements. You might do something similar to this.

(I apologize if I've misunderstood your question, or did a poor job of answering it.)

11-07-2008

tabstop

And to add one little bit, the void inside the parameter list as void function(void) means that the function does not actually use any parameters.

11-07-2008

anon

This is also one of the differences between C and C++. In C++, void foo(); and void foo(void); mean exactly the same (function takes no parameters), in C the former means that the prototype leaves the number and types of arguments unspecified - the compiler will accept anything where the function is called.

Compare the following compiled as C and C++ code:

Code:

#include <stdio.h>

void foo(); //compiles with this in C, but not in C++
//void foo(void); //does not compile with this in C nor C++

int main (void)
{
foo(1, "Hello");
return 0;
}

void foo(int n, const char* str)
{
printf("&#37;s %d\n", str, n);
}

11-07-2008

Magos

Dont forget void* pointers, which has absolutely nothing to do with void :)

11-07-2008

CornedBee

Quote:

Originally Posted by Magos

Dont forget void* pointers, which has absolutely nothing to do with void :)

Not true, if you use a proper definition of void.

Void is the non-type. There cannot be an object of type void, because void, if you treat is as a type, has no possible values.

Think of it this way: since memory is limited, it is theoretically possible enumerate every single value a variable could possibly have. So, for example, an unsigned int can have the values 0, 1, 2, and so on up to UINT_MAX (for 32-bit integers, that's 2^32 - 1). A signed int can have INT_MIN ... -1, 0, 1 ... INT_MAX. Even a float is enumerable, although knowing just which values it can enumerate means knowing how floating point numbers work, and how exactly the float representation works.
A struct can be enumerated. The struct

Same for arrays, and thus strings. Enums obviously can be enumerated, since they are defined by enumerating their possible values.

We could go even further. We could say that a type is defined by the enumeration of its values.
(This is not formally true. A type is defined by its canonical name. There are many instances in C++ where two differently named types have the same set of values. However, it may help understanding what's to come.)

Now, every normal type in C++ has at least one possible value. Void doesn't. If empty enums were allowed, you could get something very similar to void by doing:

Code:

enum void {}

There is no way to give an object of type void a value, since there aren't any values of type void. Void can be defined as the type that has no values.

With me so far?

Now, let's get into the practical uses of void. The most common use is for a function that doesn't return a value. Let's first look at a function that does return a value:

This function says, "I'll return one of void's possible values, i.e. none." In other words, the function returns no value.

See how wonderfully consistent this is?

The next important use is in void pointers. Let's again examine the normal pointer first.

Code:

unsigned int* iptr;

iptr here is a variable of type "pointer to unsigned int".
The "pointer to" part means that it contains a memory address.
The "unsigned int" part means that at this memory address, one of unsigned int's possible values is stored, i.e. 0, 1, 2, ... UINT_MAX.
So this is a pointer you can dereference, so that you can then read the value there.

Code:

void* vptr;

This is a "pointer to void".
The "pointer to" part again means that it contains a memory address.
The "void" part means that at this address, there's one of void's possible values, i.e. none. This is a bit trickier than the functions above. The pointer is saying that there isn't a value where it points to. On the other hand, as long as it's not a null pointer, there is some memory there, which has a bit pattern. You can't dereference the void pointer, since you wouldn't get a value. However, you can cast the pointer and thus give an interpretation to the bit pattern.

Finally, there's the argument list void. This one is a bit of a hack. Formally, it says, "I take no value as the first argument". This is then reinterpreted as, "I take no values as arguments at all", probably since it would be stupid to take nothing as the first argument but something as a subsequent argument.

As you can see, it's not something totally different at all. Void fits beautifully into the general type system of C and C++.