Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C array initialization via macro

Tags:

arrays

c

macros

Question's Background:
void dash(int *n, char c) is to draw characters c separated by '+'.
Parameter n is an array of ints, e.g. {1, 3, 2} and '-' for c should give "+-+---+--+", which works fine. To use dash I do {int f={1, 3, 2}; dash(f, '-');}, which makes the construct copy&pastable.

The question itself:
To avoid copy&pasting I wanted to do #define F(X, Y) {int f=X; dash(f, Y);}, resulting in a nicely usable F({1, 3, 2}, '-').
Unfortunately the compiler complains about F getting 4 (array's length + 1) arguments instead of 2.

So how can you give {1, 3, 2} as parameter to a macro?

like image 365
spc-mrn Avatar asked Oct 31 '25 00:10

spc-mrn


1 Answers

Variadic macros are a feature of C99.

#define F(Y,...) dash((int[]){__VA_ARGS__},Y)

So how can you give {1, 3, 2} as parameter to a macro?

F('-',1,3,2);
like image 53
Will Avatar answered Nov 01 '25 12:11

Will