Home > Back-end >  How do you check what parameter was passed onto a function?
How do you check what parameter was passed onto a function?

Time:08-18

I want to check what is the name of the variable that is passed onto a function, so like:

void foo(char v[8][8]) {
    if(name of passed array == 'v')
        //do something
    else if(name of passed array == 'w')
        //do something else

Would this even work? I have seen the same question asked but it was for python and they used "is" (a keyword). Is there an equivalent for c ?

CodePudding user response:

C cant do this.

If you really want to do this, create a struct with the meta-data:

struct A
{
char name;
char v[8][8];
}

void foo(A &a)
{
 if(a.name == 'v'){
 //do something
 }
 else(a.name == 'w')
 {
 //something else
 }
}

CodePudding user response:

I expand my idea on macros, written in a comment by me previously:

#define foo_wrapper(exp) \
if (0 == strcmp(#exp, "w")){...}\
foo(exp);

Here is info on stringification of expressions with macros: https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Stringification.html

  • Related