Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

C and C++ are strongly typed languages, meaning that blocks of memory given a specific type (i.e., int, float, char, etc.) when allocated or used within a piece of code. Sometimes, it is desirable to write functions that can perform the
same operations on different variable types. In C++, this is achievable through class inheritance. However,
in C, this is done by telling the compiler not to check the type of the variable; this is done with the void*
declaration. Specifically, void* is a pointer to untyped memory, and the programmer is free to cast this memory
into any other type:

float fcn( void* var1, void* var2 ) {
float* f1 = (float*)var1;
float* f2 = (float*)var2;
/* return the product of these two floats */
return (*f1)*(*f2);
}

...