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!

Discussions

pointers - Why does void in C mean not void? - Software Engineering Stack Exchange
In strongly-typed languages like Java and C#, void (or Void) as a return type for a method seem to mean: This method doesn't return anything. Nothing. No return. You will not receive anything fro... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
August 22, 2014
What does (void**) mean in C? - Stack Overflow
Which confuses the heck out of me cause all of those look familiar to me; I've just never seen them put together like that before. What does it mean? I am a newb so the less jargon, the better, thanks! ... void* is a "pointer to anything". void ** is another level of indirection - "pointer ... More on stackoverflow.com
🌐 stackoverflow.com
void pointers in generic linked list
void * is a pointer type. You shouldn't use it to store integers. More on reddit.com
🌐 r/C_Programming
9
2
November 2, 2020
Double void pointer gives "incompatible pointer types"
The type int * is not compatible with void ** because int is not compatible with void *, in turn because int is not a pointer type. More on reddit.com
🌐 r/C_Programming
8
1
November 18, 2020

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 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. The following program compiles and runs fine.
Published   3 weeks ago
🌐
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 ...
🌐
W3Schools
w3schools.com › c › c_functions.php
C Functions
void myFunction() { printf("I just got executed!"); } int main() { myFunction(); myFunction(); myFunction(); return 0; } // I just got executed! // I just got executed! // I just got executed! Try it Yourself » · You can put almost whatever you want inside a function.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_void_pointer.htm
void Pointer in C
C - Pointers vs. Multi-dimensional Arrays ... A void pointer in C is a type of pointer that is not associated with any data type. A void pointer can hold an address of any type and can be typecasted to any type.
🌐
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 - Internships Jobs Compete Mentorship Courses Practice ... A void pointer in C is a pointer that does not have a specific type, i.e., it can point to variables of multiple data types.
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.

🌐
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.
🌐
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
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Void-Pointers.html
Void Pointers (GNU C Language Manual)
Next: Pointer Comparison, Previous: Dereferencing Null or Invalid Pointers, Up: Pointers [Contents][Index] The peculiar type void *, a pointer whose target type is void, is used often in C. It represents a pointer to we-don’t-say-what.
🌐
cppreference.com
en.cppreference.com › w › c › keyword › void
C keywords: void - cppreference.com
October 24, 2017 - C language · [edit] Keywords · [edit] void type: as the declaration of the incomplete type · void: in a function with no parameter or no return value · Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/keyword/void&oldid=96392" Support us ·
Top answer
1 of 6
17

void* is a "pointer to anything". void ** is another level of indirection - "pointer to pointer to anything". Basically, you pass that in when you want to allow the function to return a pointer of any type.

&variable takes the address of variable. variable should already be some kind of a pointer for that to work, but it's probably not void * - it might be, say int *, so taking its address would result in a int **. If the function takes void ** then you need to cast to that type.

(Of course, it needs to actually return an object of the right type, otherwise calling code will fail down the track when it tries to use it the wrong way.)

2 of 6
8

Take it apart piece by piece...

myFunction takes a pointer to a pointer of type void (which pretty much means it could point to anything). It might be declared something like this:

myFunction(void **something);

Anything you pass in has to have that type. So you take the address of a pointer, and cast it with (void**) to make it be a void pointer. (Basically stripping it of any idea about what it points to - which the compiler might whine about otherwise.)

This means that &variable is the address (& does this) of a pointer - so variable is a pointer. To what? Who knows!

Here is a more complete snippet, to give an idea of how this fits together:

#include <stdio.h>

int myInteger = 1;
int myOtherInt = 2;
int *myPointer = &myInteger;

myFunction(void **something){
    *something = &myOtherInt;
}

main(){
    printf("Address:%p Value:%d\n", myPointer, *myPointer);
    myFunction((void**)&myPointer);
    printf("Address:%p Value:%d\n", myPointer, *myPointer);
}

If you compile and run this, it should give this sort of output:

Address:0x601020 Value:1
Address:0x601024 Value:2

You can see that myFunction changed the value of myPointer - which it could only do because it was passed the address of the pointer.

🌐
Code with C
codewithc.com › code with c › blog › understanding void type in c programming
Understanding Void Type In C Programming - Code With C
January 12, 2024 - Let’s kick things off with the basics. So, what on earth is the Void Type? 🤔 Well, my friends, in C, "void" is a keyword used to indicate that a function does not return any value. Yes, you heard that right!
🌐
Learn C++
learncpp.com › cpp-tutorial › void
4.2 — Void – Learn C++
void is intentionally incomplete since it represents the lack of a type, and thus cannot be defined.
🌐
Quora
quora.com › Can-you-explain-the-difference-between-void-and-static-void-functions-in-the-C-language-When-should-each-one-be-used-and-why
Can you explain the difference between 'void' and 'static void' functions in the C language? When should each one be used and why? - Quora
Answer (1 of 3): The void keyword, in this context, refers to the type of data the function returns to the caller. Using void as the return type indicates that the function returns nothing to the caller — not in a return value, anyway.
🌐
Quora
quora.com › Why-is-the-main-void-in-C
Why is the main void in C? - Quora
Answer (1 of 5): When you define any C function to return void, you’re saying that the function doesn’t need to return anything to its caller through the return value mechanism. The caller invokes your function to do some work, but doesn’t need a status or other type of information returned by th...
🌐
Jim Fisher
jameshfisher.com › 2016 › 11 › 27 › c-void
What is `void` in C?
November 27, 2016 - In the declaration void* baz, the void signifies that “baz points to a value of unknown type”. It is confusing to call void a “type”. There are positions where a normal type (int) can be used, but void cannot. For example, int foo; is a valid declaration, but void foo; is not.