简体   繁体   English

在C中动态更改结构属性

[英]Change the structure attributes dynamically in c

I have a question in C. I have a structure whose definition is like this. 我在C中有一个问题。我有一个定义如下的结构。

struct c1 
{
    int a ; 
    int b ; 
#ifdef _UNION_DEFINED
    union {
#endif
       float c; 
       float d;
#ifdef _UNION_DEFINED
    };
#endif
};

The above structure will enable the union if the flag _UNION_DEFINED is enabled. 如果启用了标志_UNION_DEFINED,则以上结构将启用联合。 But I want to achieve the same thing dynamically during the run time. 但是我想在运行时动态地实现同一件事。 Like if a function returns true, then the union should be enabled, else the union should not be defined. 就像函数返回true一样,则应启用并集,否则不应定义并集。 Can anyone please tell me if this could be acheived. 任何人都可以告诉我是否可以实现。

Thanks, d 感谢:D

No, that can't be achieved. 不,那是无法实现的。 C is a typically a static, compiled language. C是典型的静态编译语言。 This means that when the program runs, there is no meta information (like type information) left, all there is is the raw machine code. 这意味着在程序运行时,没有元数据信息(如类型信息),只有原始的机器代码。

You can perhaps define both structures, then at run-time choose the proper one as needed. 您也许可以定义两个结构,然后在运行时根据需要选择适当的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM