联盟在C中的应用 [英] Application of Union in C
问题描述
我想在C中使用union的应用。请解释
I want what is the application of union in C.Please explain
推荐答案
2006-04-19,balasam< ; BK ***** @ gmail.com>写道:
On 2006-04-19, balasam <bk*****@gmail.com> wrote:
我想在C中使用union的什么应用。请解释
I want what is the application of union in C.Please explain
他们经常被用于某种多态性。
例如,如果您正在为动态类型的
语言编写解释器,则可以使用类似的联合来表示语言中的值
这个:
enum value_type
{
INT,
REAL,
STRING,
OBJECT
};
struct value
{
enum value_type type;
union
{
int val_int;
double val_real;
char * val_string;
struct object * val_object;
}
值;
};
所有值都是struct value的实例,但可以代表
int,double,char或object。
They''re quite often used for a sort of "polymorphism".
For example, if you were writing an interpreter for a dynamically typed
language, you might represent values in the language using unions like
this:
enum value_type
{
INT,
REAL,
STRING,
OBJECT
};
struct value
{
enum value_type type;
union
{
int val_int;
double val_real;
char *val_string;
struct object *val_object;
}
value;
};
All values are instances of "struct value", but that can represent an
int, a double, a char or an object.
union的一个常见用途是简化分解输入的STREAM,在下面的
示例中将int值读入两个短值。
>
/ * tunion.c * /
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
/ *用法:tunion< binfile * /
int
main(int argc ,char * argv [])
{
union {
unsigned ii;
struct {
短高;
短低;
}字;
}解码;
printf(" sizeof(decode)=%d \ n",sizeof(decode));
printf(" sizeof(decode.ii)=%d \ n" ,sizeof(decode.ii));
printf(" sizeof(decode.word)=%d \ n",sizeof(decode.word));
printf(" sizeof(decode.word.high)=
%d \ n",sizeof(decode.word.high));
printf(" ; sizeof(decode.word.low)=
%d \ n& quot;,sizeof(decode.word.low));
/ *从标准输入读取0x01020304 * /
读取(0,& decode.ii ,sizeof(decode.ii));
printf(" high word =%04hx \ n",decode.word.high);
printf(" low word =%04hx \ n",decode.word.low);
/ *从标准输入读取0x04030201 * /
读取(0,& decode.ii,sizeof(decode.ii));
printf(" high word =%04hx \ n",decode.word.high);
printf(" low word =%04hx \ n",decode.word.low);
}
重定向一个包含8个字节的文件,你可以使用union
变量解码来直接访问int
值读取的两个16位字。 / b
binfile包含:^ A ^ B ^ C ^ D ^ D ^ C ^ B ^ A (报价不在档案中)
CJ
A common use for union is to ease breaking up a STREAM of input, in the
example below breaking an int value read into two short values.
/* tunion.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* Usage: tunion <binfile */
int
main(int argc,char *argv[])
{
union {
unsigned ii;
struct {
short high;
short low;
} word;
} decode;
printf("sizeof(decode) = %d\n",sizeof(decode));
printf("sizeof(decode.ii) = %d\n",sizeof(decode.ii));
printf("sizeof(decode.word) = %d\n",sizeof(decode.word));
printf("sizeof(decode.word.high) =
%d\n",sizeof(decode.word.high));
printf("sizeof(decode.word.low) =
%d\n",sizeof(decode.word.low));
/* Reads 0x01020304 from stdin */
read(0,&decode.ii,sizeof(decode.ii));
printf("high word = %04hx\n",decode.word.high);
printf("low word = %04hx\n",decode.word.low);
/* Reads 0x04030201 from stdin */
read(0,&decode.ii,sizeof(decode.ii));
printf("high word = %04hx\n",decode.word.high);
printf("low word = %04hx\n",decode.word.low);
}
Redirecting a file containing eight bytes, below, you can use the union
variable decode to get direct access to the two 16 bit words of the int
value read.
binfile contains: "^A^B^C^D^D^C^B^A" (quotes not in file)
CJ
这篇关于联盟在C中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!