freonix freonix - 1 year ago 85
C Question

Passing variable type as function parameter

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
and using macros?

Answer Source

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; \
    ... \