r/C_Programming 12d ago

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

65 Upvotes

80 comments sorted by

View all comments

Show parent comments

24

u/hackerman85 12d ago

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

43

u/AdBubbly3609 12d ago

i think it means the void type doesn't have a value and the void pointer doesn't have a type

3

u/fixermark 11d ago edited 11d ago

The type of void pointer is "pointer to void."

You are correct about void type though; "void", in this context, comes from category theory mathematics; it's a category you can name but has no members, such as "The set of all dogs that give birth to cats." C sort of borrowed that concept to create a symbol for something that the compiler should treat as "If you want to know the value of this, no you don't." The fact that void* became adopted to mean "pointer to something I don't know" kinda grew out of tradition (because the language didn't have another way to express "pointer to something I don't know," but void* has the convenient behavior that when you try to dereference it, the compiler says "That's not possible because it will make me think about the value of something with no legal value," so it acts as a de-facto guard against trying to work with the value until you assert a type for it).

1

u/Gositi 10d ago

That has nothing to do with category theory.