w.jayson
w.jayson

Reputation: 37

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.

Upvotes: 4

Views: 2851

Answers (2)

w.jayson
w.jayson

Reputation: 37

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.

Upvotes: 0

Jens
Jens

Reputation: 72619

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".

Upvotes: 7

Related Questions