工会访问 [英] union access

查看:74
本文介绍了工会访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct ntt {

int type;

union {

int i;

char * s ;

};

} nt;


nt n;

ni = 0;


我发现了一个这样的C例子并且无法获得gcc 2.95.4来编译它

(struct没有名为`i'的成员)直到我宣布工会的一个实例:

union {

int i;

char * s;

} u ;


,访问方式如下:

nui = 0;


第一个例子有效吗?如果是这样,问题出在哪里?


-

Sean Dolan

typedef struct ntt {
int type;
union {
int i;
char* s;
};
}nt;

nt n;
n.i = 0;

I found a C example like this and could not get gcc 2.95.4 to compile it
(struct has no member named `i'') until I declared an instance of the union:
union {
int i;
char* s;
}u;

and accessed like:
n.u.i = 0;

Is the first example valid? If so, where is the problem?

--
Sean Dolan

推荐答案

Sean Dolan写道:
Sean Dolan wrote:
typedef struct ntt {
int type;
union {
int i;
char * s;
};
} nt;
typedef struct ntt {
int type;
union {
int i;
char* s;
};
}nt;




这称为匿名联盟,它是Microsoft扩展。虽然

gcc支持带有-fms-extensions标志的MS扩展,但你可以更好地将它转换为标准C.你可以按照你的建议这样做。如果你想要避免修复所有引用,你可以简单地处理

联合,这个类型的每个对象花费额外的单词 - 这个

在您的应用中可能是也可能不是什么大不了的事。如果联盟中的名称

是全球唯一的,您可以使用宏将它们扩展到

完全合格的版本。

- -

Derrick Coetzee

我将此新闻组发布到公共领域。我不承担所有

明示或暗示保证并承担所有责任。我不是专业人士。



This is called an anonymous union and it is a Microsoft extension. While
gcc supports MS extensions with the flag -fms-extensions, you''d do
better converting it to standard C. You can do this as you suggested. If
you want to avoid fixing up all references, you can simply dispose of
the union, costing an extra word or so per object of this type - which
may or may not be a big deal in your app. If the names inside the union
are globally unique, you can use macros to expand them out to their
fully-qualified versions.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.


2004-09-14,Derrick Coetzee< dc **** @ moonflare.com>写道:
On 2004-09-14, Derrick Coetzee <dc****@moonflare.com> wrote:
这称为匿名联盟,它是Microsoft扩展。虽然
gcc支持带有-fms-extensions标志的MS扩展,但你可以更好地将其转换为标准C.你可以按照你的建议这样做。如果你想避免修复所有引用,你可以简单地处理这个联合,每个这类对象花费额外的单词 - 这可能是也可能不是很大处理你的应用程序。如果联盟内的名称是全球唯一的,您可以使用宏将它们扩展为完全合格的版本。
This is called an anonymous union and it is a Microsoft extension. While
gcc supports MS extensions with the flag -fms-extensions, you''d do
better converting it to standard C. You can do this as you suggested. If
you want to avoid fixing up all references, you can simply dispose of
the union, costing an extra word or so per object of this type - which
may or may not be a big deal in your app. If the names inside the union
are globally unique, you can use macros to expand them out to their
fully-qualified versions.




谢谢你是Derrick,它清理了它。


-

Sean Dolan



Thank you Derrick, that cleared it right up.

--
Sean Dolan


Sean Dolan < SE ***** @ bellsouth.net>写道:
Sean Dolan <se*****@bellsouth.net> writes:
typedef struct ntt {
int type;
union {
int i;
char * s;
};
} nt;

nt n;
ni = 0;

我发现了一个像这样的C例子并且无法获得gcc 2.95.4来编译它
(struct没有名为`i'的成员),直到我声明了一个联盟的实例:
union {
int i;
char * s;
} u;

和访问类似:
nui = 0;

第一个例子有效吗?如果是这样,问题出在哪里?
typedef struct ntt {
int type;
union {
int i;
char* s;
};
}nt;

nt n;
n.i = 0;

I found a C example like this and could not get gcc 2.95.4 to compile it
(struct has no member named `i'') until I declared an instance of the union:
union {
int i;
char* s;
}u;

and accessed like:
n.u.i = 0;

Is the first example valid? If so, where is the problem?




不,第一个例子无效。在会员声明中


int type;


" int"是成员类型和类型是是会员的名字。


在(尝试过的)会员声明中


union {int i; char * s; };


" union {int i; char * s; }"是会员类型,但没有会员

名称。 (子工会和子结构并没有平放在周围结构或联合的名称空间中。)


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/ ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



No, the first example is not valid. In the member declaration

int type;

"int" is the member type and "type" is the name of the member.

In the (attempted) member declaration

union { int i; char *s; };

"union { int i; char *s; }" is the member type, but there is no member
name. (Sub-unions and sub-structs don''t flatten into the namespace of
the surrounding struct or union.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于工会访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆