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?
You don't actually pass a type to a function here, but create new code for every time you use it. To avoid doubt: ({...}) is a "statement expression", which is a GCC extension and not standard C.
In JavaScript, you cannot pass parameters by reference; that is, if you pass a variable to a function, its value is copied and handed to the function (pass by value). Therefore, the function can't change the variable. If you need to do so, you must wrap the value of the variable (e.g., in an array).
If you want the called method to change the value of the argument, you must pass it by reference, using the ref or out keyword. You may also use the in keyword to pass a value parameter by reference to avoid the copy while guaranteeing that the value will not be changed. For simplicity, the following examples use ref .
There are two ways to pass arguments to a function: by reference or by value. Modifying an argument that's passed by reference is reflected globally, but modifying an argument that's passed by value is reflected only inside the function.
You could make an enum for all different types possible, and use a switch to make the dereferencing:
typedef enum { CHAR, INT, FLOAT, DOUBLE } TYPE; void foo(TYPE t, void* x){ switch(t){ case CHAR: (char*)x; break; case INT: (int*)x; break; ... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With