这合法吗? [英] Is this legal ?

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

问题描述

大家好,


我想要一个有两个结构的联盟。

所以我这样做了,


union {

struct {

int a;

int b;

};

struct {

浮动c;

};

}联合国;


我不想命名这两个内部结构,因为我想

访问这样的成员,

< union name>。< member姓名>

而不是

< union name>。< structure name>。< member name>


My问题是,这是有效的C吗?

我还观察到,如果我有这样的工会,


union {

struct {

int a;

int b;

};

struct {

浮动a;

浮动b;

};

}联合国;


编译器没有给我任何错误。

如果我说''un.a''怎么知道我是否使用浮动a或者$

a?

感谢您的时间,

Yugi。

解决方案

main()写道:


大家好,


我想要一个其中有两个结构的联盟。

所以我这样做了,


union {

struct {

int a;

int b;

};

struct {

float c;

};

}联合国;


我不想命名这两个内部结构,因为我想要

访问这样的成员,

< union name>。< member name>

而不是

< union name>。< structure name>。< member name>


我的问题是,这是有效的C吗?



编号当您编写< union name>时,编译器无法理解并意识到

。< member name>

你的意思是< union name>。< some structure>。< member name>


>

我还观察到,如果我有这样的工会,


union {

struct {

int a;

int b;

};

struct {

浮动a;

浮动b;

};

}联合国;


编译器没有给我任何错误。

如果我说''un.a''怎么知道我是否使用浮动a或int

a?



它不会。因此,即使它的定义也没用,但是b $ b并不会导致编译器抱怨。对于

计划


int main(无效){

union {

struct {

int a;

int b;

};

struct {

float a;

浮动b;

};

}联合国;

}

Sun编译器提供

" b.c",第6行:警告:未命名的工会成员

" b.c",第10行:警告:未命名的工会成员

" b.c",第11行:零大小的struct / union

cc:acomp因bc而失败

gcc -Wall -ansi -pedantic bc给出

bc:在函数`main'':

bc:6:警告:ANSI C禁止成员声明没有成员

bc:6:警告:未命名的struct / union定义没有实例

bc:10:警告:ANSI C禁止没有成员的成员声明

bc:10:警告:未命名的struct / union定义没有实例

bc:11:警告:union没有成员

bc:11:警告: ü nused变量`un''

bc:12:警告:控制到达无效功能的结束


我建议你转动你的编译器''警告

升级。如果你仍然没有收到警告,那么转换到新的编译器是时候了。


>


感谢您的时间,

Yugi。




Spiros Bousbouras写道:


main()写道:


大家好,


我想要一个有两个结构的联盟。

所以我这样做了,


union {

struct {

int a;

int b;

};

struct {

float c;

};

}联合国;


我不想命名两个内部结构,因为我想

访问这样的成员,

< union name>。< member name>

而不是

< union name>。< structure name>。< member姓名>


我的问题是,这是有效的C吗?



编号当您编写< union name>。时,编译器无法理解并意识到

。< member name>

你的意思是< union name>。< some structure>。< member name>



我也观察到如果我有这样的工会,


union {

struct {

int a;

int b;

};

struct {

浮动a;

浮动b;

};

}联合国;


编译器没有给我任何错误。

如果我说''un.a''怎么知道我是否使用浮动a或int

a?



它不会。因此,即使它的定义也没用,但是b $ b并不会导致编译器抱怨。对于

计划


int main(无效){

union {

struct {

int a;

int b;

};

struct {

float a;

浮动b;

};

}联合国;

}

Sun编译器提供

" b.c",第6行:警告:未命名的工会成员

" b.c",第10行:警告:未命名的工会成员

" b.c",第11行:零大小的struct / union

cc:acomp因bc而失败

gcc -Wall -ansi -pedantic bc给出

bc:在函数`main'':

bc:6:警告:ANSI C禁止成员声明没有成员

bc:6:警告:未命名的struct / union定义没有实例

bc:10:警告:ANSI C禁止没有成员的成员声明

bc:10:警告:未命名的struct / union,它不定义实例
bc:11:警告:工会没有会员

bc:11:警告:未使用的变量`un''

bc:12:警告:控制到达无效功能的结束


我建议你把你的编译器警告

升级。如果您仍然没有收到警告,那么转换到新编译器的时间是




感谢您的时间,

Yugi。



Annonymous Union的主要用途是限制某些名称的范围

空间。如果你要定义Anno。工会,你必须自己照顾

变量的范围。


这不是严格有效的C但它是常见的扩展。

lcc-win32将接受此代码,并且它是实用的,因为

它允许您避免指定整个路径。

会员并在不更改代码的情况下为结构的

布局提供更大的灵活性。


jacob

Hi all,

I wanted to have a union which has two structures in it.
So i did this,

union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

I did not want to name the two inner structures, because i wanted to
access the members like this,
<union name>.<member name>
instead of
<union name>.<structure name>.<member name>

My question is, Is this valid C?

I also observed that if i have a union like this ,

union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;

Compiler doesn''t give me any error.
If i say, ''un.a'' how will it know whether i''m using float a or int
a ?
Thanks for your time,
Yugi.

解决方案

main() wrote:

Hi all,

I wanted to have a union which has two structures in it.
So i did this,

union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

I did not want to name the two inner structures, because i wanted to
access the members like this,
<union name>.<member name>
instead of
<union name>.<structure name>.<member name>

My question is, Is this valid C?

No. The compiler cannot read your mind and realize
that when you write <union name>.<member name>
you really mean <union name>.<some structure>.<member name>

>
I also observed that if i have a union like this ,

union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;

Compiler doesn''t give me any error.
If i say, ''un.a'' how will it know whether i''m using float a or int
a ?

It won''t. Hence your definition is useless even if it
doesn''t cause your compiler to complain. For the
programme

int main(void) {
union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;
}

Sun compiler gives
"b.c", line 6: warning: unnamed union member
"b.c", line 10: warning: unnamed union member
"b.c", line 11: zero-sized struct/union
cc: acomp failed for b.c

gcc -Wall -ansi -pedantic b.c gives
b.c: In function `main'':
b.c:6: warning: ANSI C forbids member declarations with no members
b.c:6: warning: unnamed struct/union that defines no instances
b.c:10: warning: ANSI C forbids member declarations with no members
b.c:10: warning: unnamed struct/union that defines no instances
b.c:11: warning: union has no members
b.c:11: warning: unused variable `un''
b.c:12: warning: control reaches end of non-void function

I would suggest that you turn your compiler''s warning
levels up. If you still don''t get a warning then it''s time
to switch to a new compiler.

>

Thanks for your time,
Yugi.



Spiros Bousbouras wrote:

main() wrote:

Hi all,

I wanted to have a union which has two structures in it.
So i did this,

union {
struct{
int a;
int b;
};
struct{
float c;
};
}un;

I did not want to name the two inner structures, because i wanted to
access the members like this,
<union name>.<member name>
instead of
<union name>.<structure name>.<member name>

My question is, Is this valid C?


No. The compiler cannot read your mind and realize
that when you write <union name>.<member name>
you really mean <union name>.<some structure>.<member name>


I also observed that if i have a union like this ,

union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;

Compiler doesn''t give me any error.
If i say, ''un.a'' how will it know whether i''m using float a or int
a ?


It won''t. Hence your definition is useless even if it
doesn''t cause your compiler to complain. For the
programme

int main(void) {
union {
struct{
int a;
int b;
};
struct{
float a;
float b;
};
}un;
}

Sun compiler gives
"b.c", line 6: warning: unnamed union member
"b.c", line 10: warning: unnamed union member
"b.c", line 11: zero-sized struct/union
cc: acomp failed for b.c

gcc -Wall -ansi -pedantic b.c gives
b.c: In function `main'':
b.c:6: warning: ANSI C forbids member declarations with no members
b.c:6: warning: unnamed struct/union that defines no instances
b.c:10: warning: ANSI C forbids member declarations with no members
b.c:10: warning: unnamed struct/union that defines no instances
b.c:11: warning: union has no members
b.c:11: warning: unused variable `un''
b.c:12: warning: control reaches end of non-void function

I would suggest that you turn your compiler''s warning
levels up. If you still don''t get a warning then it''s time
to switch to a new compiler.



Thanks for your time,
Yugi.


The main usage of Annonymous Union is to limit scope of some name
space. If you are defining Anno. union, you have to take care scope of
variable yourself.


This is not strictly valid C but it is a common extension.
lcc-win32 will accept this code, and it is practical since
it allows you to avoid specifying the whole "path" to
the member and giving you more flexibility in the
layout of the structire without changing the code.

jacob


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

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