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

🌐
Reddit
reddit.com › r/c_programming › void()
r/C_Programming on Reddit: void()
June 28, 2023 -

I am trying to learn C for the first time.

Can someone please explain to me when it is necessary to use the void function? I don't understand what is meant by it doesn't return a value.

Top answer
1 of 3
13
A return value is the data that the function evaluates to when it completes. If you have a function like this: int multiply(int x, int y) { return x * y; } Then you can write something like this: int result = multiply(6, 7); multiply will run, and the return value will be an int with the value of 42, which will then be assigned to the variable result. When you declare and define a function, you need to specify the return type, because the compiler needs to know how much space to use to store the return value, as well as making sure that you're not assigning the value to a mismatched type, or using it where a function expects a different type. In the case of a void function, you're telling the compiler that your function is not going to return any value at all. void say_hello(char *name) { printf("Hello, %s!\n", name); } In this case you just run the function, and it prints the message and then ends. There's no return value that the function gives you that you can use elsewhere in your program. ie. this would work: say_hello("George"); but this would not: int result = say_hello("George");
2 of 3
12
Void itself is not a function. It is used to define void functions, which are functions that don't return a value. You use it whenever you want to write a function that does not return a value. In some other languages, void functions are called procedures. For example, if you write a logging function, its purpose is to output data to a file. It doesn't calculate or read any information, so it doesn't need to return anything to the caller. The same keyword, void, is also used to define that a function does not take any parameters. So void hello(void) { /* ... */ } is a function that does not take any input parameters, and does not return any value. Finally, there are void pointers, but I think that's for another time.
🌐
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 ...