Basically it means "nothing" or "no type"

There are 3 basic ways that void is used:

  1. Function argument: int myFunc(void) -- the function takes nothing.

  2. Function return value: void myFunc(int) -- the function returns nothing

  3. Generic data pointer: void* data -- 'data' is a pointer to data of unknown type, and cannot be dereferenced

Note: the void in a function argument is optional in C++, so int myFunc() is exactly the same as int myFunc(void), and it is left out completely in C#. It is always required for a return value.

Answer from Gerald on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/explainlikeimfive โ€บ eli5: what is the purpose of void in c?
r/explainlikeimfive on Reddit: ELI5: What is the purpose of void in C?
March 8, 2024 -

I just began studying C and I cannot, for the life of me, understand void.

I have read and listened to many people say "It does not return a value". Ok? What is a value? Why wouldn't we want to return it? What is the difference between "void main" and "int main"? Can we just use int for everything and ignore void altogether?

In what situations is void used? In what situations is void better? etc. Please help I don't get it at all!

Basically it means "nothing" or "no type"

There are 3 basic ways that void is used:

  1. Function argument: int myFunc(void) -- the function takes nothing.

  2. Function return value: void myFunc(int) -- the function returns nothing

  3. Generic data pointer: void* data -- 'data' is a pointer to data of unknown type, and cannot be dereferenced

Note: the void in a function argument is optional in C++, so int myFunc() is exactly the same as int myFunc(void), and it is left out completely in C#. It is always required for a return value.

Answer from Gerald on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
void pointers in C are used to implement generic functions in C. For example, compare function which is used in qsort(). void pointers used along with Function pointers of type void (*)(void) point to the functions that take any arguments and ...
Published ย  July 17, 2014
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Void_type
Void type - Wikipedia
2 weeks ago - The void type, in several programming languages, more so curly bracket programming languages derived from C and ALGOL 68, is the return type of a function that returns normally, but provides no result value to its caller. Usually such functions are called for their side effects, such as performing ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-void-keyword-in-c
What is the void keyword in C?
The literal meaning of void is empty or blank. In C, void can be used as a data type that represents no data.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_void_pointer.htm
void Pointer in C
A void pointer can hold an address of any type and can be typecasted to any type. They are also called general-purpose or generic pointers. In C programming, the function malloc() and calloc() return "void *" or generic pointers. This is the syntax you would use to declare a void pointer โˆ’
Top answer
1 of 11
60

The keyword void (not a pointer) means "nothing" in those languages. This is consistent.

As you noted, void* means "pointer to anything" in languages that support raw pointers (C and C++). This is an unfortunate decision because as you mentioned, it does make void mean two different things.

I have not been able to find the historical reason behind reusing void to mean "nothing" and "anything" in different contexts, however C does this in several other places. For example, static has different purposes in different contexts. There is obviously precedent in the C language for reusing keywords this way, regardless of what one may think of the practice.

Java and C# are different enough to make a clean break to correct some of these issues. Java and "safe" C# also do not allow raw pointers and do not need easy C compatibility (Unsafe C# does allow pointers but the vast majority of C# code does not fall into this category). This allows them to change things up a bit without worrying about backwards compatibility. One way of doing this is introducing a class Object at the root of the hierarchy from which all classes inherit, so an Object reference serves the same function of void* without the nastiness of type issues and raw memory management.

2 of 11
32

void and void* are two different things. void in C means exactly the same thing as it does in Java, an absence of a return value. A void* is a pointer with an absence of a type.

All pointers in C need to be able to be dereferenced. If you dereferenced a void*, what type would you expect to get? Remember C pointers don't carry any runtime type information, so the type must be known at compile time.

Given that context, the only thing you can logically do with a dereferenced void* is ignore it, which is exactly the behavior the void type denotes.

Find elsewhere
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Void-Pointers.html
Void Pointers (GNU C Language Manual)
The functions for dynamic memory allocation (see Dynamic Memory Allocation) use type void * to refer to blocks of memory, regardless of what sort of data the program stores in those blocks. With type void *, you can pass the pointer around and test whether it is null.
๐ŸŒ
Crasseux
crasseux.com โ€บ books โ€บ ctutorial โ€บ void.html
void - The GNU C Programming Tutorial - at Crasseux (?)
The void data type was introduced to make C syntactically consistent. The main reason for void is to declare functions that have no return value. The word "void" is therefore used in the sense of "empty" rather than that of "invalid".
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-void-958182
What Is the Definition of "Void" in C and C++?
April 28, 2019 - When used in a function's parameter list, void indicates that the function takes no parameters. Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value ...
๐ŸŒ
Quora
quora.com โ€บ What-is-the-use-of-void-in-c
What is the use of void in c? - Quora
Answer (1 of 12): The general meaning for the word โ€œvoidโ€ is โ€œnot valid or completely emptyโ€. This term plays a vital role in computer programming. The most familiar uses are: ยท Void as a Function Return Type ยท Void as a Function Parameter ยท Void as a Pointer Declaration 1) Void as a Function...
Top answer
1 of 11
253

A pointer to void is a "generic" pointer type. A void * can be converted to any other pointer type without an explicit cast. You cannot dereference a void * or do pointer arithmetic with it; you must convert it to a pointer to a complete data type first.

void * is often used in places where you need to be able to work with different pointer types in the same code. One commonly cited example is the library function qsort:

void qsort(void *base, size_t nmemb, size_t size, 
           int (*compar)(const void *, const void *));

base is the address of an array, nmemb is the number of elements in the array, size is the size of each element, and compar is a pointer to a function that compares two elements of the array. It gets called like so:

int iArr[10];
double dArr[30];
long lArr[50];
...
qsort(iArr, sizeof iArr/sizeof iArr[0], sizeof iArr[0], compareInt);
qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareDouble);
qsort(lArr, sizeof lArr/sizeof lArr[0], sizeof lArr[0], compareLong);

The array expressions iArr, dArr, and lArr are implicitly converted from array types to pointer types in the function call, and each is implicitly converted from "pointer to int/double/long" to "pointer to void".

The comparison functions would look something like:

int compareInt(const void *lhs, const void *rhs)
{
  const int *x = lhs;  // convert void * to int * by assignment
  const int *y = rhs;

  if (*x > *y) return 1;
  if (*x == *y) return 0;
  return -1;
}

By accepting void *, qsort can work with arrays of any type.

The disadvantage of using void * is that you throw type safety out the window and into oncoming traffic. There's nothing to protect you from using the wrong comparison routine:

qsort(dArr, sizeof dArr/sizeof dArr[0], sizeof dArr[0], compareInt);

compareInt is expecting its arguments to be pointing to ints, but is actually working with doubles. There's no way to catch this problem at compile time; you'll just wind up with a missorted array.

2 of 11
30

Using a void * means that the function can take a pointer that doesn't need to be a specific type. For example, in socket functions, you have

send(void * pData, int nLength)

this means you can call it in many ways, for example

char * data = "blah";
send(data, strlen(data));

POINT p;
p.x = 1;
p.y = 2;
send(&p, sizeof(POINT));
๐ŸŒ
C-programming-simple-steps
c-programming-simple-steps.com โ€บ what-is-void.html
What is void in C - C Programming
Master C programming in simple steps! Newbie or advanced you can do this in simple steps! Here are the tutorials and examples that will show you how.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ void-mean-in-c-cpp-and-csharp
What Does void Mean in C, C++, and C# โ€“ Linux Hint
In C, you use the void to declare a function that does not return any value (no return argument). For instance, a function that displays a message or performs a task without producing a result is usually declared void. An example of such a function in C is shown below:
๐ŸŒ
Quora
quora.com โ€บ Why-do-we-use-void-in-C-programming-functions-instead-of-format-specifiers
Why do we use void in C programming functions, instead of format specifiers? - Quora
Answer (1 of 7): Void is one of those language smells in C. They tried to make everything functions, but not variables and constants which are also functions. Routines that donโ€™t return anything are not functions. C is really confused over functions. https://www.quora.com/Does-a-function-always-...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2756726 โ€บ what-is-the-use-of-void-before-the-function
what is the use of void before the function? | Sololearn: Learn to code for FREE!
Overall, `void` is an important keyword in C and C++ programming languages, as it is used to indicate functions that do not return any values or take any arguments.
๐ŸŒ
BYJUS
byjus.com โ€บ gate โ€บ void-pointer-in-c
Void Pointer in C
August 1, 2022 - In simpler words, it will only be able to point to the float type variable. We thus use a pointer to void to overcome this very problem. The pointer to void can be used in generic functions in C because it is capable of pointing to any data type.
๐ŸŒ
Florida State University
cs.fsu.edu โ€บ ~cop3014p โ€บ lectures โ€บ ch7 โ€บ index.html
Functions 2: Void (NonValue-Returning) Functions
Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does ยท ...
๐ŸŒ
Fresh2Refresh
fresh2refresh.com โ€บ home โ€บ c programming tutorial โ€บ c interview questions โ€บ what is void in c?
What is void in C? | C Interview Questions | Fresh2Refresh.com
October 15, 2020 - Void is an empty data type that has no value. We use void data type in functions when we donโ€™t want to return any value to the calling function.