1

For Example. I have this function -> Sort(void * param) in which there is a generic parameter. I need to understand what is the type of the parameter passed cause the sorting of an Int is different from a char. So I need a similar situation :

Sort(void *param){
     if(param is int)
        //some code
     else if(param is char)
        //some code
     else if //ecc
}

I don't know how to write the code inside if statements.

cocoricò
  • 51
  • 4
  • I suggest you look at how the library function `qsort()` manages without knowing the type. Apart from being given the element size, it also needs a use-case `compare()` function (which also uses `void*`). – Weather Vane Nov 21 '20 at 17:08
  • May be this link helps you -> https://stackoverflow.com/questions/6280055/how-do-i-check-if-a-variable-is-of-a-certain-type-compare-two-types-in-c – Kozmotronik Nov 21 '20 at 18:38

1 Answers1

0

Type of param is void*. Address was casted to it during function call and you cannot just "revert" it back to original type. Instead you could pass additional information to function, e.g.:

void Sort(void* param, char type)
{
    if (type == 'i') {
        // some code for `int`
    } else if (type == 'c') {
        // some code for `char`
    } else if (type == 'f') {
        // some code for `float`
    } else {
        // some code ...
    }
}
Jorengarenar
  • 2,705
  • 5
  • 23
  • 60