Reputation: 93
I have a function like this:
foo(int a[], int b);
and I want to print the name of the array in the function. If I call
foo(cards, 5);
I want to print this: array name:cards;array size:5
. How should I do this?
Upvotes: 0
Views: 119
Reputation: 40145
To create a wrapper macro.
#define STR(v) #v
#define FOO(name, value) do{ fprintf(stderr, "array name:%s;array size:%d\n", STR(name), value);foo(name, value); }while(0)
Use FOO(cards, 5);
instead of.
Upvotes: 1
Reputation: 212198
You can't. By the time your program is executing, the name "cards" that was used in the source code is no longer available. But you can do:
void foo(int *a, int b, const char *name);
...
foo(cards, 5, "cards");
Upvotes: 3