如何使用宏将字符串转换为变量名? [英] 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;
}
在将字符串"Hello"
转换为变量名时遇到麻烦.
例如:"Hello"
应该通过使用宏转换为const char* Hello;
.由于有双引号,我无法将其转换.这是我在堆栈溢出的第一个问题.
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.
推荐答案
您不能在C语言中解串"字符串.
You can't "destringify" a string in C.
不过,您可以对令牌进行字符串化,因此解决方案是采用另一种方法:使用令牌hello
并在需要"hello"
时对其进行字符串化.
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"
.
这篇关于如何使用宏将字符串转换为变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文