简体   繁体   中英

How to access structure member with stringification (using ## or # in C macro)?

typedef enum
{
    ENUM1_A=0,
    ENUM1_B,
}someEnum1_e;

typedef union
{
    someEnum1_e value;
}someEnum1_e_t;


#define GET_ELEMENT(data_name) blah.#data_name    

int main (void)
{
    someEnum1_e_t  blah = {ENUM1_A};

    printf("val = %d ",GET_ELEMENT(value));
}

returns:

main.c: In function ‘main’:
main.c:27:41: error: expected identifier before string constant
     printf("val = %d ",GET_ELEMENT(value));
                                         ^
main.c:21:38: note: in definition of macro ‘GET_ELEMENT’
 #define GET_ELEMENT(data_name) blah.#data_name  

How can I access to blah.value using ## or # in C macro ?

不要使用#

#define GET_ELEMENT(data_name) blah.data_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM