Tee
Tee

Reputation: 93

How to print array name in function from argument?

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

Answers (2)

BLUEPIXY
BLUEPIXY

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

William Pursell
William Pursell

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

Related Questions