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!

People also ask

Is there any disadvantage to using the void pointer in C?
The void pointer is pretty viable and safe to use in a code, and it makes things very easy to manage. But, its polymorphism due to the void * can be unsafe. Once we cast any pointer to the void*, we cannot prevent it from being cast to the wrong pointer by fault, in case there is an error in the program.
๐ŸŒ
byjus.com
byjus.com โ€บ gate โ€บ void-pointer-in-c
Void Pointer in C
Why do we use a void pointer in C programs?
We use the void pointers to overcome the issue of assigning separate values to different data types in a program. The pointer to void can be used in generic functions in C because it is capable of pointing to any data type. One can assign the void pointer with any data typeโ€™s address, and then assign the void pointer to any pointer without even performing some sort of explicit typecasting. So, it reduces complications in a code.
๐ŸŒ
byjus.com
byjus.com โ€บ gate โ€บ void-pointer-in-c
Void Pointer in C
What is the difference between a general pointer and a void pointer in C?
They are both the same. The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
๐ŸŒ
byjus.com
byjus.com โ€บ gate โ€บ void-pointer-in-c
Void Pointer in C
๐ŸŒ
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.

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
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ definition-of-void-958182
What Is the Definition of "Void" in C and C++?
April 28, 2019 - The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement. For example, a function that prints a message doesn't return a value.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Void_type
Void type - Wikipedia
2 weeks ago - The explicit use of void vs. giving no arguments in a function prototype had different semantics in C and C++, as detailed in this table: The C syntax to declare a (non-variadic) function with an as-yet-unspecified number of parameters, e.g. void f() above, was deprecated in C99.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ void-pointer-c-cpp
void Pointer in C - GeeksforGeeks
If you're looking to master pointers, including how they work with data structures, the C Programming Course Online with Data Structures covers pointers extensively with practical use cases. ... The following program doesn't compile. ... // C Program to demonstrate that a void pointer // cannot be dereferenced #include <stdio.h> int main() { int a = 10; void* ptr = &a; printf("%d", *ptr); return 0; } ... The below program demonstrates the usage of a void pointer to store the address of an integer variable and the void pointer is typecasted to an integer pointer and then dereferenced to access the value.
Published ย  July 17, 2014
Find elsewhere
๐ŸŒ
Florida State University
cs.fsu.edu โ€บ ~cop3014p โ€บ lectures โ€บ ch7 โ€บ index.html
Functions 2: Void (NonValue-Returning) Functions
//value-returning function call (assignment): y = 2.0 * sqrt(x); In contrast, a void function (method or procedure, in other languages) does not return a function value. Nor is it called from within an expression. Instead, the function call appears as a complete, stand-alone statement.
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));
๐ŸŒ
BYJUS
byjus.com โ€บ gate โ€บ void-pointer-in-c
Void Pointer in C
August 1, 2022 - Here, the void keyword acts as the pointer type, and it is followed by the pointer name- to which the pointer type points and allocates the address location in the code. The declaration of a pointer happens with the name and type of pointer that supports any given data type. Let us take a look at an example ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-void-keyword-in-c
What is the void keyword in C?
Note: In C, foo() is different from foo(void). foo() means that the function takes an unspecified number of arguments. foo(void) is used for a function that takes no arguments. Generic pointer declaration that has no type specified with it.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_void_pointer.htm
void Pointer in C
Hence, an array of void pointers is an array that can store memory addresses, but these addresses can point to variables of different data types. You can also store pointers to any user-defined data types such as arrays and structures in a void pointer. In this example program, we have declared an array of void pointers and stored in it the pointers to variables of different types (int, float, and char *) in each of its subscripts.
๐ŸŒ
Crasseux
crasseux.com โ€บ books โ€บ ctutorial โ€บ void.html
void - The GNU C Programming Tutorial - at Crasseux (?)
Although the data returned by a ... to write a function that does not return a value, simply declare it void. A function declared void has no return value and simply returns with the command return;....
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Void-Pointers.html
Void Pointers (GNU C Language Manual)
The peculiar type void *, a pointer ... is void, is used often in C. It represents a pointer to we-donโ€™t-say-what. Thus, ... declares a function numbered_slot_pointer that takes an integer parameter and returns a pointer, but we donโ€™t say what type of data it points to. 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 ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Void Functions | C Programming Tutorial - YouTube
An explanation of what void functions are in C and how to use them. Source code: https://github.com/portfoliocourses/c-example-code/blob/main/void_functions...
Published ย  July 17, 2024
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ void pointer in c explained in detail with code examples
Void Pointer In C Explained In Detail With Code Examples // Unstop
March 1, 2024 - This method involves merely declaring the pointer variable with the syntax void * type. Here, we do not assign any specific location to the pointer. This is why the process if referred to as defining a void pointer in C without initializing it. It is useful if we want to assign an address at a later time, as we did in the example before.
๐ŸŒ
HowDev
how.dev โ€บ answers โ€บ what-is-the-void-keyword-in-c
What is the void keyword in C?
Note: In C, foo() is different from foo(void). foo() means that the function takes an unspecified number of arguments. foo(void) is used for a function that takes no arguments. Generic pointer declaration that has no type specified with it.
๐ŸŒ
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:
๐ŸŒ
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 - What is void in C? - 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 etc.
๐ŸŒ
Quora
quora.com โ€บ Can-you-explain-why-someone-might-want-to-use-void-instead-of-int-for-their-functions-return-type-in-the-C-programming-language-Are-there-any-specific-examples-where-this-might-be-useful-or-necessary
Can you explain why someone might want to use void instead of int for their function's return type in the C programming language? Are there any specific examples where this might be useful or necessary? - Quora
Answer (1 of 4): Question: Can you explain why someone might want to use void instead of int for their function's return type in the C programming language? Are there any specific examples where this might be useful or necessary? Easy - Do you need to return anything? If yes, why the hell would ...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ eli5: void in c
r/C_Programming on Reddit: ELI5: 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!