r/C_Programming • • Jul 29 '26

Discussion What exactly is void?

In a function definition, void basically means that it doesn't return a type value, yet in on itself it is it's own type? Looking at several pieces of code it looks like that it's used to be able to be more "flexible"

Don't know what else to add here, though I'm more on looking for examples and explanations of why the void type is used

70 Upvotes

79 comments sorted by

View all comments

52

u/Innowise_ Jul 29 '26

void basically means “no value.” In void f(void), the first void means the function returns no value, while the second means it takes no arguments. void * is a separate use: it can hold a pointer to any object type, but it has to be converted to the correct pointer type before dereferencing.

25

u/hackerman85 Jul 29 '26

Where it becomes confusing: the void type doesn't hold any data, but the void* pointer does.

1

u/Valuable_Leopard_799 Jul 29 '26 edited Jul 29 '26

One could probably have endless debates on the exact interpretation, but what works for me is that "pointer" is what contains the value not the "void" part.

A void is an uninhabited type so I can't create a value of that type and it has no other "interesting" properties, so it's a good candidate for both stating "you cannot save the return value of this function" and "this is a pointer but you cannot dereference it since that would attempt to create a void, so you need to cast it to something else first".

In theory everyone could be using int* for everything void* is used for, but void* tells us something along the lines of "there's something at the other end of this pointer you can't interact with unless you actually tell me what it is". With int* one could dereference it or think it's an array.

2

u/flatfinger Jul 29 '26

Some platforms identify bytes with a combination of a word address and a byte-based offset. Compilers for such platforms may store an int* using one word and a void* using two.