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
... 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; }
Published ย  July 17, 2014
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
Is there any difference between the null pointer and the void pointer in C?
The null pointer is basically used in a program to assign the value 0 to a pointer variable of any data type. The void pointer, on the other hand, has no value assigned to it and we use it to store the addresses of other variables in the program- irrespective of their data types.
๐ŸŒ
byjus.com
byjus.com โ€บ gate โ€บ void-pointer-in-c
Void Pointer in C
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-void-keyword-in-c
What is the void keyword in C?
Return type of a function that returns nothing. Consider the code snippet below, which uses void as a return type. ... Input parameter of a function that takes no parameters.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_void_pointer.htm
void Pointer in C
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.
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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!

๐ŸŒ
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. One example is the get function associated with the istream and ifstream classes:
๐ŸŒ
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 ...
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Void-Pointers.html
Void Pointers (GNU C Language Manual)
{ int *p; /* Converts return value ... has a pointer type also converts. For example, supposing the function hack is declared to require type float * for its parameter, this call to hack will convert the argument to that type....
๐ŸŒ
E Computer Notes
ecomputernotes.com โ€บ home โ€บ pointer โ€บ void functions in c
Void Functions in C - Computer Notes
October 31, 2020 - void Write (void) { printf("You need a compiler for learning C language.\n"); } The first line in the above definition may also be written as ยท void Write () Program presents an example where a void function is defined to display a message. Illustrates a void function with void parameter list.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Void_type
Void type - Wikipedia
2 weeks ago - The C language standard does not guarantee that the different pointer types have the same size or alignment. A function with void result type ends either by reaching the end of the function or by executing a return statement with no returned value. The void type may also replace the argument list of a function prototype to indicate that the function takes no arguments.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ void-pointer-in-c
Void Pointer in C - javatpoint
% -&gt; . # Show Answer The correct option is (b). Explanation: For a structure, Arrow ( -&gt;) is used... ... What is a Null Pointer? A Null Pointer is a pointer that does not point to any memory location. It stores the base address of the segment. The null pointer basically stores the Null value while void is the type of the pointer. A null pointer is a... ... Till now, we have seen that in C programming, we can pass the variables as an argument to 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));
๐ŸŒ
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.
๐ŸŒ
HackerEarth
hackerearth.com โ€บ practice โ€บ notes โ€บ void-pointer-in-c
void pointer in C | HackerEarth
A void pointer can hold address of any type and can be typcasted to any type. int a = 10; char b = 'x'; void *p = &a; // void pointer holds address of int 'a' p = &b; // void pointer holds address of char 'b'
๐ŸŒ
University of Utah
users.cs.utah.edu โ€บ ~germain โ€บ PPS โ€บ Topics โ€บ C_Language โ€บ c_functions.html
C Programming - Functions
In C, the default is to pass by value. For example: // // C function using pass by value. (Notice no &) // void doit( int x ) { x = 5; } // // Test function for passing by value (i.e., making a copy) // int main() { int z = 27; doit( z ); printf("z is now %d\n", z); return 0; }
๐ŸŒ
Crasseux
crasseux.com โ€บ books โ€บ ctutorial โ€บ void.html
void - The GNU C Programming Tutorial - at Crasseux (?)
A variable that is itself declared void (such as my_variable above) is useless; it cannot be assigned a value, cannot be cast to another type, in fact, cannot be used in any way.