I know that void
type means no value at all https://www.gnu.org/software/c-intro-and-ref/manual/html_node/The-Void-Type.html in C. But, I have seen some code in C in which some parameter variables were passed as void
pointer :
return_type my_function_signature(void* param1, int num_count, double real_value){
type_to_cast_to var_param1 = (type_to_cast_to *)param1;
// write something here
enter code here
return return_value;
}
I wonder, what's the use of having void* param1
in parameter list, when in the body of the function one knows exactly the type to cast ?
It's not like template functions in C++ https://en.cppreference.com/w/cpp/language/variable_template.
Any help from the connoisseurs of C and C++ ?
Using a void *
as a parameter can be useful for callback functions, where the exact type of the parameter isn't necessarily known to the function that takes the callback, but is known to the programmer who instantiates it.
It can also be useful when working on generic data when the exact type isn't known.
A well-known example of both is the qsort
function from the standard library which has the following declaration:
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
Here, the base
parameter points to the start of an array, while the function pointer compar
takes pointers to two objects to compare. In both cases the qsort
function does not know what the array/element type is.
Suppose you wanted to sort an array of int
and an array of float
. You would create the following callback functions:
int comp_int(const void *p1, const void *p2)
{
const int *i1 = p1;
const int *i2 = p2;
if (*i1 < *i2) {
return -1;
} else if (*i1 > *i2) {
return 1;
} else {
return 0;
}
}
int comp_float(const void *p1, const void *p2)
{
const float *f1 = p1;
const float *f2 = p2;
if (*f1 < *f2) {
return -1;
} else if (*f1 > *f2) {
return 1;
} else {
return 0;
}
}
And the qsort
function would be called like this:
int a1[5] = { ... };
float a2[10] = { ... };
qsort(a1, 5, sizeof *a1, comp_int);
qsort(a2, 10, sizeof *a2, comp_float);