3
#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.

Jens
  • 69,818
  • 15
  • 125
  • 179
w.jayson
  • 27
  • 5
  • 1
    why a string ? Why not `TRACE(Hello)` ? – Sander De Dycker Jun 21 '18 at 07:05
  • yAlso, here your `TRACE` has exactly one macro argument: `arg1` is bound to `("Hello",a)`. – Antti Haapala -- Слава Україні Jun 21 '18 at 07:12
  • since TRACE(Hello) is used in another macro am using as TRACE(("Hello")) and more over , can use printf function. So I'm retaining it. – w.jayson Jun 21 '18 at 07:19
  • what do you mean by "used in another macro" ? What do you mean by "can use printf function" ? Can you show code for both to clarify what you want to achieve and what your restrictions are ? – Sander De Dycker Jun 21 '18 at 07:55
  • `#define SMM_TRACE(x) printf x` `#define SMM_TRACE_CONVERT SMM_TRACE(CONVERT_ERROR_KEY_FAILED)` `int main()` `{` `int num = 1;` `SMM_TRACE(("the number is %d\n", num));` `printf("%s","SMM_TRACE_CONVERT\n");` `return 0;` `}` – w.jayson Jun 21 '18 at 09:42
  • This is the thing I have mentioned as "used in another macro" and "can use printf function" I want to achieve `static const char* Hello` from `("Hello")` using a macro. my restriction is if i eliminate inner inner parentheses in macro, then `Hello` can me made in to a variable. – w.jayson Jun 21 '18 at 09:49
  • you might want to look into [`__VA_ARGS__`](https://stackoverflow.com/questions/26053959/what-does-va-args-in-a-macro-mean) - ie. `#define SMM_TRACE(...) printf(__VA_ARGS__)`, and just get rid of those inner parentheses. – Sander De Dycker Jun 21 '18 at 10:08
  • If i use `#define SMM_TRACE(...)` then `TRACE(("Hello",a));` will act as a single argument. `("Hello",a)` as one argument for which I will not be able to split `Hello` and `a`. – w.jayson Jun 21 '18 at 11:00
  • Smells like another solution in search of a problem to solve. Can you provide some more context by editing your question? Because this is most likely the wrong solution to whatever it is you are doing. – Lundin Jun 21 '18 at 11:56
  • @Lundin I'm expecting output as a variable of `static const char* Hello` from `TRACE(("Hello",a));` is there any possible way to get the output. – w.jayson Jun 21 '18 at 12:36
  • @w.jayson Why are you using macros to declare variables? – Lundin Jun 21 '18 at 12:46
  • macro was used for tracing purpose. it was occupying more memory. when i convert it to a variable it will be using less memory so only. – w.jayson Jun 21 '18 at 13:12
  • If you are concerned about memory use then no 1 is to drop printf and variadic functions. Apart from that it rather sounds like what you need is just a compound literal. – Lundin Jun 21 '18 at 14:47
  • @Lundin Thats sounds good. – w.jayson Jun 22 '18 at 04:15

2 Answers2

6

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

Jens
  • 69,818
  • 15
  • 125
  • 179
  • ok I will use the token hello and I ll stringify it when needed. another problem is the inner parenthesis (). With parentheses (hello) will not be a variable. How to avoid this. is there any way to eliminate this. – w.jayson Jun 21 '18 at 09:23
  • @w.jayson You should search for "c variadic macros" if that is what you need. They are part of the C language starting with C99. See, e.g., https://en.wikipedia.org/wiki/Variadic_macro – Jens Jun 21 '18 at 10:11
  • Yes, am trying to implement variadic macros. The only thing I dint get is, is it possible to use `#define TRACE((arg1,...))` instead of `#define TRACE(arg1,...)` so that I can easily avoid the restrictions. – w.jayson Jun 21 '18 at 10:57
0

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.

w.jayson
  • 27
  • 5