In C++, void
is a keyword that has several uses. Here are some of them:
- Function return type: When used as a function return type,
void
specifies that the function doesnt return a value. - Function parameter list: When used for a functions parameter list,
void
specifies that the function takes no parameters. - Pointer declaration: When used in the declaration of a pointer,
void
specifies that the pointer is "universal." If a pointers type isvoid*
, the pointer can point to any variable thats not declared with theconst
orvolatile
keyword. Avoid*
pointer cant be dereferenced unless its cast to another type. Avoid*
pointer can be converted into any other type of data pointer. - Void pointers:
void
has a third, more advanced use in C++ that involves void pointers. Void pointers are pointers that can point to any type of data, but they cannot be dereferenced unless they are cast to another type.
In summary, void
is used to indicate that a function does not return a value, that a function takes no parameters, or that a pointer is universal and can point to any type of data.