freonix freonix - 1 month ago 7x
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?


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