3

I'm writing a complex macro and I need to pass also array initializer. Basically I have trouble to do:

#define INIT_ARR(VAR_NAME,ARR_DATA) int VAR_NAME[] = ARR_DATA

then I would call it

INIT_ARR(myNm,{1,2,3});

but preprocessors interprets any commas (also the one inside curly braces) as new macro parameter so it gives me error:

error:  #55-D: too many arguments in invocation of macro "INIT_ARR" 

preprocessor does not ignore () so I can do:

#define INIT_ARR(VAR_NAME,ARR_DATA) int VAR_NAME[] = {ARR_DATA}
INIT_ARR(myNm,(1,2,3));

but then it is interpreted as

int myNm[] = {(1,2,3)};

which is not correct for C.

Is there a way how to do it?? For example remove braces from parameter?

3
  • Possible duplicate of Array format for #define (C preprocessor) Commented Jul 26, 2017 at 11:21
  • Why do you try to obfuscate your code? There is no need for the macro; if you need a custom type name, use a typedef (but as you hav to know how to initialise, this also is not very useful). Don't get too fancy with macros. Commented Jul 26, 2017 at 12:34
  • it is not part of question. But it is test code, which test one function with many parameters, and I give expected system state as array initializer. Basically it is 4 lines of code ret = tested_funct(PARA,PARB,...); if(ret!=0) return PAR_X | ret; ret = chkModuleState({EXP_STATE}) return PAR_Y | ret; And this repeats 50 times... So I use macro for that. Calling macro repeatedly looks like a table and it is very nice to read what states are tested. True debugging is little obscured... Commented Jul 27, 2017 at 7:48

2 Answers 2

8

I think I cracked it:

#define myArgs(...) __VA_ARGS__
#define INIT_ARR(VAR_NAME,ARR_DATA) int VAR_NAME[] = {myArgs ARR_DATA}
INIT_ARR(myArr,(1,2,3,4));

will be interpreted correctly as:

int myArr[] = {1,2,3,4};

annoying_squid's answer helped me to figure it out...

Sign up to request clarification or add additional context in comments.

2 Comments

Good, but this is not a hack, just a normal behaviour of C Preprocessor. :)
yes. To me calling of myArgs macro seems not so obvious. In first step it will be just set to myArgs (1,2,3,4) which will be executed and turn into 1,2,3,4. Luckily the space between myArgs and (1,2,3,4) is not a problem. And luckily it is interpreted correctly recursively... But glad you say it is "not a hack". I will use your comment when my boss will ask me what is that ;)
7

You can use variable number of arguments with the macro as -

#define INIT_ARR(VAR_NAME, ...) int VAR_NAME[] = {__VA_ARGS__}

2 Comments

Sorry I modified questions. I did not mention originally that initializer is only part of my macro arguments. I have more arguments so your answer is not valid after my modification... IT seems it is working when modified slightly: #define INIT_ARR2(VAR_NAME,...) int VAR_NAME[] = {__VA_ARGS__}. Thx for effort I up you, but I like mine solution better as it keeps at least parenthesis over initialize.
Whatever suits your needs better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.