0

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?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Tee
  • 91
  • 2
  • 9

2 Answers2

3

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");
William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

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.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70