[英]Dynamically use a structure in c
In my code there are multiple structures like structure_1, structure_2, structure_3 etc. I am trying to write a macro which will enable me to select the correct structure (which structure to select is based on another value).在我的代码中有多个结构,如结构_1、结构_2、结构_3等。我正在尝试编写一个宏,它将使我能够 select 正确的结构(select 的结构基于另一个值)。 I can't reveal the exact code...but its similar to this我不能透露确切的代码......但它类似于这个
#include <stdio.h>
#define concat(X,Y) X##_##Y
#define typename(X){\
if(val==0)\
concat(X,1)\
else\
concat(X,2)\
}
int val;
typedef struct {
int x;
char c;
}structure_1;
typedef struct {
int y;
char c;
}structure_2;
int main() {
val = 0;
typename(structure) abc;
abc.x = 0;
printf("%d\n",abc.x);
}
This doesn't work and is giving issues这不起作用并且会出现问题
24 11 E:\DEV\concat.c [Error] expected expression before 'structure_1'
3 22 E:\DEV\concat.c [Note] in definition of macro 'concat'
24 2 E:\DEV\concat.c [Note] in expansion of macro 'typename'
25 2 E:\DEV\concat.c [Error] 'abc' undeclared (first use in this function)
25 2 E:\DEV\concat.c [Note] each undeclared identifier is reported only once for each function it appears in
Using if-else statement is an option, but it'll have to be done for many structures and in many places, so i want to avoid that.使用 if-else 语句是一种选择,但它必须在许多结构和许多地方完成,所以我想避免这种情况。 Please suggest what i am doing wrong or any other way of doing it.请建议我做错了什么或任何其他方式。
The reason your macro doesn't work is because it results in if (val == 0) structure_1;
您的宏不起作用的原因是它导致if (val == 0) structure_1;
which won't compile because you haven't declared a variable.它不会编译,因为你没有声明一个变量。 The abc
part isn't injected into the macro, so it only appears at the end of the processed macro expansion: abc
only gets declared as a variable of structure_2
. abc
部分没有注入到宏中,所以它只出现在已处理的宏扩展的末尾: abc
只被声明为structure_2
的变量。
This is easily enough fixed: you have to have the variable name as a parameter to your macro as well.这很容易解决:您还必须将变量名称作为宏的参数。 Do that and... now the very point has obviously become ruined because your variables exist only inside the if-else blocks which is what you didn't want.这样做并且......现在这一点显然已经被破坏了,因为你的变量只存在于 if-else 块中,这是你不想要的。
It's at this point that it should become obvious that this simply isn't how C works fundamentally.在这一点上,很明显这根本不是 C 的工作原理。 Genericism isn't well supported, which is a good thing because genericism isn't relevant to what C is trying to accomplish as a language.泛型没有得到很好的支持,这是一件好事,因为泛型与 C 作为一种语言试图实现的目标无关。 You should ask yourself why you are trying to force the language to behave this way.你应该问问自己为什么要强迫语言以这种方式表现。 What is the point of what you are trying to do?您尝试做的事情有什么意义?
Regardless, there is a way to get around this in some sense: Unions.无论如何,从某种意义上说,有一种方法可以解决这个问题:工会。 Unions allow you to essentially smack different types together into one type.联合允许您基本上将不同的类型组合成一种类型。 This has the distinct drawback of forcing variables declared from a union to always be the size of the largest type.这有一个明显的缺点,即强制从联合声明的变量始终是最大类型的大小。 If you make a union from a 1-byte char and a 100-byte struct, every variable declared will always consume 100-bytes.如果你从一个 1 字节的 char 和一个 100 字节的结构体中创建一个联合,则声明的每个变量将总是消耗 100 字节。 On the other hand, if you are smacking together same-sized structs, there is no particular penalty.另一方面,如果您将相同大小的结构拼凑在一起,则没有特别的惩罚。
A simple demonstration:一个简单的演示:
struct t {
union {
int x; float y;
};
};
int main() {
struct t a = {0};
printf("%d\n", a.x);
printf("%d\n", a.y);
printf("%d\n", sizeof(a)); // 4
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.