What happened was:
In 1972 C was invented. In C function names must be unique so if you wrote a function to display an int:
-
void display(int arg)
-
{
-
printf("%d", arg);
-
}
-
then no one could use the name
display for a funciton to display a float.
That forced function design to be not specific about the data and that caused the function to be written with a pointer to the data. A pointer to data whose type is not known is a void*. That is, it's a generic address. Now the function looks like:
-
void display(void* arg)
-
{
-
printf("%d", *arg);
-
}
-
which won't compile since you cannot dereference an address unless the address has a type. Like, int* dereferences to int so a void* would have to dereference to a void and a void means no type. Since all data must have type, the result is you are not allowed to dereference a void pointer.
Now you have to tell the function what the tyoe of the argument is by using a second argument. The function can then typecast the void pointer:
-
void display(void* arg, int parm)
-
{
-
int* temp_int;
-
float* temp_float;
-
switch(parm)
-
{
-
case 1:
-
temp_int = (int*)arg;
-
printf("%d", *temp_int);
-
break;
-
case 2:
-
temp_float = (float*)arg;
-
printf("%f", *temp_float);
-
break;
-
case 3:
-
/* you get the idea */
-
-
}
-
This is really ugly and it forces you to change the function each time you have a new type of data to display. If the function has been distributed tio customers, then you have to upgrade all those customers.
C++ solves this problem with function overloading. Here the function name and the arguments must be unique rather than just the name.
oler1s says void* are not heavily used in C++. I would like to strengthen that by saying
void* is not to be used in C++. void* is in C++ largely to be compatile with C and to allow you to use C functions if you really have to.