3

I am trying to come up with a macro to do something like the following,

MY_MACRO(type,name,args...)
MY_MACRO(int,_array,1,2,3)

and expand to,

int _array[] = {1,2,3};
coll* __array = my_call(&array[0],&array[1],&array[2]);

is this possible with/without compiler specific magic?

my_call expects variable number of arguments and I do not want to pass the array directly.

EDIT: I am already using the accepted answer from the following SO question for var args,

Macro returning the number of arguments it is given in C?

So I can find how many elements are in the array.

Community
  • 1
  • 1
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
  • What is it that your macro does after all? – karlphillip Feb 09 '11 at 18:28
  • What you want is not possible for an arbitrary number of arguments, exactly as written, but I'm confused why you want to pass individual pointers to each array element to the function call. Just passing a pointer to the first element would be just as good. – R.. GitHub STOP HELPING ICE Feb 09 '11 at 18:34

1 Answers1

4

You can start with (C99, not GCC-specific):

#define MY_MACRO(type, name, ...) \
  type name[] = {__VA_ARGS__};

The my_call part will be more difficult; there are macros online that can count the number of arguments, but you will need something like Boost.Preprocessor (which should work in C) to apply my_call to the consecutive elements of the array. How many elements do you have as a maximum? You might want something like:

#define COUNT(...) COUNT2(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define COUNT2(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, count, ...) count
#define PP_CAT(a, b) PP_CAT2(a, b)
#define PP_CAT2(a, b) a ## b
#define CALL_MY_FUNC(arr, ...) my_call(PP_CAT(ITER_, COUNT(__VA_ARGS__))(arr));
#define ITER_0(arr) /**/
#define ITER_1(arr) (arr)
#define ITER_2(arr) (arr), ITER_1((arr) + 1)
#define ITER_3(arr) (arr), ITER_2((arr) + 1)
#define ITER_4(arr) (arr), ITER_3((arr) + 1)

and so on.

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
  • I edited the answer a couple of minutes ago to try to solve the second part of your problem. Please see if that works too. – Jeremiah Willcock Feb 09 '11 at 18:48
  • Edited the question I have a macro in place to count the number of arguments from another so question but I am stuck at calling my_call – Hamza Yerlikaya Feb 09 '11 at 18:49
  • I showed a brute-force solution in my answer. It is probably good enough to solve your problem. – Jeremiah Willcock Feb 09 '11 at 18:50
  • I just put in something a little better for the iteration part. – Jeremiah Willcock Feb 09 '11 at 18:55
  • Instead of boost, which is designed for C++, you may use P99 which uses va_arg macros and the trick you are pointing to in your question to count the arguments and then do "code unfolding". See e.g the answer that I have given here: http://stackoverflow.com/questions/4904255/how-can-i-generate-a-list-via-the-c-preprocessor-cpp/4906083#4906083 – Jens Gustedt Feb 09 '11 at 20:21
  • Either should work; one of the answers to that question says that Boost.Preprocessor will work in C too; anything similar in P99 should work as well. Note that the code I put doesn't use Boost at all. – Jeremiah Willcock Feb 09 '11 at 20:24