如何将字符串转换为变量名?例如。我有一个字符串列表:
"str1", "str2", "str3", etc.
和一个结构:
struct my_struct {
int str1;
int str2;
int str3;
} m = {5, 10, 15, ... etc};
给定一个字符串
"str2"
, 我想打印与它相关的变量姓名
m.str2
. C 有没有办法做到这一点?谢谢!
最佳答案
这在 C 中根本不可能。
查看此问题了解更多详情。
How to return a variable name and assign the value of the variable returned in c
引用 Wichert 的话,'我建议你重新考虑你试图解决的问题,并检查是否可能没有更好的方法来解决它。也许使用数组、映射或哈希表可能是一种适合您的替代方法。
关于C - 将字符串转换为变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778923/