简体   繁体   中英

How to convert a string to variable name using macro?

#define TRACE(arg1,...)  char* arg1; 

int main(void)
{
    int a=4;
    TRACE(("Hello",a));  // convert "Hello" to a valid char variable name.
    return 0;
}

I'm having trouble in converting the string "Hello" into a variable name. for example: "Hello" should be converted as const char* Hello; by using a macro. Since there are double quotes I'm unable to convert it. This is my first question in Stack Overflow.

You can't "destringify" a string in C.

You can stringify a token, though, so the solution is to do it the other way around: use the token hello and stringify it when you need "hello" .

Thank you all of you for spending your valuable time to respond my question. Some of your comments gave me an idea to sort out the answer. you can find the answer below :

#define TRACE(arg1,...)  TRACE2 arg1
#define TRACE2(arg1, arg2) static const char arg1; \
                             printf("%p\n",(void*)&arg1);\
                              printf("%d\n",arg2);\

If any changes can be done in this code, kindly let me know.

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