用C工会的实际使用情况 [英] Actual usage of union in C
问题描述
可能重复:结果
C:在哪里?工会的实际使用
我知道联盟的概念,但我没有看到,我应该使用union数据结构现实世界编码的实际情况。我会很AP preciated,如果你们能够给我一些例子或教程显示使用正确工会的情况下。
I know the concept of union but I don't see the actual case in real world coding that I should use union data structure. I will be very appreciated if you guys can give me some examples or tutorials that shows cases using union properly.
先谢谢了。
推荐答案
当我用它不断地:在联合数据类型解析我存储所有值的配置文件。例如。当值可以是int类型或字符串,我会按如下方式使用的数据结构:
Where I use it constantly: parsing a configuration file I store all values in a union data type. E.g. when values can be int types or strings, I would use a data structure as follows:
struct cval_s {
short type;
union {
int ival;
char *cval;
} val;
};
在络合剂的问题,我使用它们了。例如。一旦我写了一个跨preTER一个简单的脚本语言,这种语言的值再次被包含联合的结构psented $ P $。
In complexer problems, I use them, too. E.g. once I wrote an interpreter for an easy scripting language, and a value in this language was represented by a struct containing a union.
这篇关于用C工会的实际使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!