First of all
header_t*
is a pointer to the type header_t
.
Answer
The pointers for the void type void*
are pointers for any type. They are used when the programmer does not know what kind of information he will receive. That is, if it is a int
, char
...
As such these can be converted to any type of pointer.
int i = 5;
void *p = &i;
*p = 4;//Valor de i agora é 4
Why the cast (header_t *) ??
For no reason. At least in C it is possible to cast void*
for any type of pointer, not adding any necessary information to the programmer (until it makes the readability of the code difficult), but in C++ it is mandatory to cast.
Example of void type return*
malloc
your return is void*
because the programmer can set whatever size he wants, so it didn’t make sense for him to have a type with defined size.
Function cast problem returning void*
If you forget to include the header file stdlib.h
and call the function malloc
you will have problems because in C the standard type is int
so would be considered int malloc()
and the cast would only hide Warning depending on the compiler.