我有一个例子,我保证了类型的对齐,union max_align.我正在寻找一个更简单的例子,其中实际上使用了union来解释我的朋友.
You*_*usf 33
我通常在解析文本时使用联合.我使用这样的东西:
typedef enum DataType { INTEGER, FLOAT_POINT, STRING } DataType ;
typedef union DataValue
{
int v_int;
float v_float;
char* v_string;
}DataValue;
typedef struct DataNode
{
DataType type;
DataValue value;
}DataNode;
void myfunct()
{
long long temp;
DataNode inputData;
inputData.type= read_some_input(&temp);
switch(inputData.type)
{
case INTEGER: inputData.value.v_int = (int)temp; break;
case FLOAT_POINT: inputData.value.v_float = (float)temp; break;
case STRING: inputData.value.v_string = (char*)temp; break;
}
}
void printDataNode(DataNode* ptr)
{
printf("I am a ");
switch(ptr->type){
case INTEGER: printf("Integer with value %d", ptr->value.v_int); break;
case FLOAT_POINT: printf("Float with value %f", ptr->value.v_float); break;
case STRING: printf("String with value %s", ptr->value.v_string); break;
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想看看HEAVILY如何使用联合,请使用flex/bison检查任何代码.例如,见splint,它包含TONS of union.
我通常使用你想要拥有不同数据视图的联合,例如32位颜色值,你需要32位val和红色,绿色,蓝色和alpha分量
struct rgba
{
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
};
union
{
unsigned int val;
rgba components;
}colorval32;
Run Code Online (Sandbox Code Playgroud)
NB您也可以通过位屏蔽和移位来实现相同的功能
#define GETR(val) ((val&0xFF000000) >> 24)
Run Code Online (Sandbox Code Playgroud)
但我觉得工会的态度更优雅
要通过将特定端口映射到内存来按字节顺序访问寄存器或I/O端口,请参阅下面的示例:
typedef Union
{
unsigned int a;
struct {
unsigned bit0 : 1,
bit1 : 1,
bit2 : 1,
bit3 : 1,
bit4 : 1,
bit5 : 1,
bit6 : 1,
bit7 : 1,
bit8 : 1,
bit9 : 1,
bit10 : 1,
bit11 : 1,
bit12 : 1,
bit13 : 1,
bit14 : 1,
bit15 : 1
} bits;
} IOREG;
# define PORTA (*(IOREG *) 0x3B)
...
unsigned int i = PORTA.a;//read bytewise
int j = PORTA.bits.bit0;//read bitwise
...
PORTA.bits.bit0 = 1;//write operation
Run Code Online (Sandbox Code Playgroud)