Thursday, February 3, 2022

[SOLVED] Passing variable type as function parameter

Issue

Is it possible to pass variable type as part of a function parameter, e.g.:

void foo(varType type)
{
  // Cast to global static
  unsigned char bar;
  bar = ((type *)(&static_array))->member;
}

I remember it has something to do with GCC's typeof and using macros?


Solution

You can't do that for a function, because then it needs to know the types of the arguments (and any other symbols the function uses) to generate working machine code. You could try a macro like:

#define foo(type_t) ({ \
    unsigned char bar; \
    bar = ((type_t*)(&static_array))->member; \
    ... \
    })


Answered By - David X
Answer Checked By - Pedro (WPSolving Volunteer)