14

Possible Duplicate:
C/C++: How to make a variadic macro (variable number of arguments)

I need macro that will expand in a array that contains it's arguments. For example:

#define foo(X0) char* array[1] = {X0}
#define foo(X0, X1) char* array[2] = {X0, X1}

and so on. My problem is that I need to use foo with variable number of arguments, so I want to be able to use foo("foo0") but also to use foo("foo0", "foo1", "foo2"..."fooN"). I know it's possible to have:

#define foo(...)
#define foo_1(X0) ..
#define foo_2(X0, X1) ..
#define foo_3(X0, X1, X2) ..
#define foo_N(X0, X1, ... XN) ..

And use ____VA_ARGS____, but I don't know how may I expand foo in foo_k macro depending on it's parameter count? Is this possible?

Community
  • 1
  • 1
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

3 Answers3

20

How about:

#define FOO( ... ) char* x[] = { __VA_ARGS__ };
JoeSlav
  • 4,479
  • 4
  • 31
  • 50
3

This should work:

#define foo(args...) char* array[] = {args}

Note that this uses a GNU extension and so will only work with gcc and gcc-compatible compilers. @JoeSlav's answer using __VA_ARGS__ is more portable.

Paul R
  • 208,748
  • 37
  • 389
  • 560
0

I recommend gcc.gnu.org docs on the subject.

Or you could jump straight away to this answer:

How to make a variadic macro (variable number of arguments)

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426