I have read somewhere a question that was about the pointer to void
or void *
and the doubt about the reserved word void
of several languages has arisen.
I have this code:
typedef enum VALUE_TYPE {
VT_INTEGER, VT_STRING, VT_BOOL, VT_NULL
} valuetype_t;
typedef struct VALUE {
void *iv;
valuetype_t tp;
} value_t;
But I don't understand much what the void *
in the is for struct
.
As far as I understand, in c and c++ , the type void
is everything and it is nothing, but I am not very clear about what this is about.
Can someone explain to me what it is void
and its functions in the various languages that use it?
Note: I've only used c and c++ because I'm not sure about using the language-agnostic tag , feel free to edit this snippet and the question tags.
For c and c++ it is true that it
void
practically means nothing , but adding a simple asterisk (*
) to it, it can be everything or, rather, Anything!Starting with parts, the reserved word
void
has the following use cases in C (among others) :Type specifier:
Above is the declaration of a function of type
void
, that is, one that does not return a value of some type and therefore cannot have a variable of typevoid
to which to assign the return of the function:And much less:
If you noticed in the first block of code, I used
(void)
in the parameters part that the method declared there uses, this is explained below.Function sealer .
By default, C functions are variadic when
void
not explicitly put between the parentheses of the function definition 1 :However, when we use
void
between the parentheses, then the above doesn't work:Delete... Warnings?
It's never a good idea to suppress the warnings your compiler throws when there are non-killing problems that can cause unspecified behavior in your software:
In this context, it
(void)
serves as a casting or conversion operator ... as we are casting to nothing, the value is useless . Man!However, there are some compilers that instead throw a warning when using the cast to
void
.What we all expected...
void *
is whatever, or... so we thought.Yes,
void
, accompanied by a*
, is a powerful data type that stores a pointer to any other existing data type, in fact, it is what the functions that reserve memory return:And this is because it
void *
is a pointer to a memory address whose type is unknown 2 , which is what you do in the code you have in the question, you define a type fieldvoid *
and guess its value by means of another variable.I really think it
void *
should be avoided or only used when the value it points to is known exactly:In fact, I can do whatever I want with that memory 3 :
In the example above, I've used it to store a 1000 byte dynamically allocated memory pointer, but since I didn't know what to do with those bytes, so I used them to store integers, likewise, I can convert that pointer to
void
to a pointer tochar
and store a text string.But what is left when we pass a pointer from anything else to a type parameter
void *
? The important thing about this is that C is somewhat permissive (I don't want it to have children) and automatically converts tovoid *
4 so that the called function can work with the pointer passed as an argument.The pointer to
void
is a very powerful type in wise hands, however, it can cause oversights and problems in the code, like any good C practice, it is recommended not to abuse this type.1 : Ideone to check it out.
2 : It's not entirely unheard of, but a pointer of type void only lets us know the address it points to, not the data it contains.
3 : Ideone to check it out.
4 : All the pointers have the same size, the only thing that changes is the way of interpreting the memory addresses according to the environment in which it runs.