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
  • 19,653
  • 7
  • 38
  • 77
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
  • 190,037
  • 45
  • 260
  • 285
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,049
  • 7
  • 31
  • 69