1

I want to create a macro-list of macro from another macro-list of macro. I don't know how to describe this, so let me introduce an example :

#include <stdio.h>

#define     MACRO_LIST_DEFAULT              \
    MACRO(TEST1, TEST2)                     \
    MACRO(TEST3, TEST4)

#define     MACRO_LIST_MODIFIED             \
    MACRO(TEST1_MODIFIED, TEST2_MODIFIED)   \
    MACRO(TEST3_MODIFIED, TEST4_MODIFIED)

#define     MACRO_LIST_FULL                 \
    MACRO_LIST_DEFAULT                      \
    MACRO_LIST_MODIFIED

#define MACRO(VAR1, VAR2)   printf("%s:%s\n", #VAR1, #VAR2);

int main(void)
{
    MACRO_LIST_FULL
}

// OUTPUT :
// TEST1:TEST2
// TEST3:TEST4
// TEST1_MODIFIED:TEST2_MODIFIED
// TEST3_MODIFIED:TEST4_MODIFIED

This work perfectly, and it is what I want to do, but I don't want to fill the "MACRO_LIST_MODIFIED" by hand, but use the previous list "MACRO_LIST_DEFAULT"

something like that in fact :

#define MACRO_LIST_MODIFIED                 \
    MACRO_LIST_DEFAULT // And add _MODIFIED to elements.

I really don't know how to deal with this problem.

thank you in advance,

2
  • Create a new program which outputs these macro definitions for you? Commented Jul 2, 2015 at 12:24
  • The "MACRO_LIST_DEFAULT" contains ~100 items and I can't have third-party program to generate the other list. I want to keep things clearly and simple with macro tricks if that can be done. Commented Jul 2, 2015 at 12:31

1 Answer 1

1

Here you go, I assume this is what you want to achieve and I assume you're using gcc:

#include <stdio.h>

#define     MACRO_LIST( x )              \
    MACRO(TEST1 ## x , TEST2 ## x )                     \
    MACRO(TEST3 ## x , TEST4 ## x )

#define     MACRO_LIST_FULL                 \
    MACRO_LIST( )                      \
    MACRO_LIST( _MODIFIED )

#define MACRO(VAR1, VAR2)   printf("%s:%s\n", #VAR1, #VAR2);

int main(void)
{
    MACRO_LIST_FULL
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was looking for a proper way to do this, this is a good one. Thank you.

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.