将union的字段传递给函数 [英] passing a union's field to a function

查看:85
本文介绍了将union的字段传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


下面的示例程序由gcc(带-Wall)编译好,但被Sun的SUNWspro编译器拒绝了

(版本6更新2)。


争论的焦点是,一个联盟的类型的值是否可以将
传递给一个函数直接 - 没有创建一个单独的变量

联合类型并指定它的相应字段。


gcc过于宽松,还是这种行为只是Sun的旧编译器不支持的更新的b / b $ C标准的一部分?谢谢!


-mi

解决方案

Mikhail Teterin说:


你好!


下面的示例程序由gcc(带-Wall)编译好,但

被Sun拒绝s SUNWspro编译器(版本6更新2)。



什么样本程序?


争论的焦点是,是否为其中一个联盟的值's
类型可以直接传递给函数 - 不需要创建union类型的单独的

变量并指定它的相应字段。



最近分配的union成员对象的值可以直接传递给

函数。没有必要将其复制出来。


gcc过于宽松,或者这种行为只是新行为的一部分

C-标准,Sun的旧编译器不支持?谢谢!



谁知道?我没有看到你的代码。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Richard Heathfield写道:
< blockquote class =post_quotes>
什么样的程序?



您的新闻阅读器必须做一些令人讨厌的附件。


该程序附在原始帖子上。这里是内联的。


-mi


#include< stdio.h>


typedef union {

???????? int ????? i;

???????? void ???? * p;

???????? struct {

??????????????? int ?????我;

????????????????????????????????????????????????????????????? } ??????? s;

} testunion;


static void

testfunc(testunion u)

{

???????? printf(" i:%d \ np:%p \ n",ui,up);

}


int

main()

{

??? ????? testfunc((testunion)3);

???????? testfunc((testunion)NULL);

????? ???返回0;

}


Mikhail Teterin说:


Richard Heathfield写道:


>什么样本程序?



您的新闻阅读器必须做一些令人讨厌的附件。



附件不会发生在comp.lang.c中 - 它是一个纯文本新闻组。


#include< stdio.h>


typedef union {

int i;

void * p;

struct {

int i;

int j;

} s;

} testunion;


static void

testfunc(testunion u)

{

printf( i:%d \ np:%p \ n,ui,up);



要么输入一个int,在这种情况下打印int是可以的,但不是

void *,或者你输入了一个空格*,在这种情况下可以打印

void *而不是int。


}


int

main()

{

testfunc((testunion)3);



这不是有效的转换。


testfunc((testunion)NULL);



也不是。


返回0;

}



-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Hello!

The sample program below is compiled fine by gcc (with -Wall), but rejected
by Sun''s SUNWspro compiler (version 6 update 2).

The point of contention is, whether a value for one of the union''s types can
be passed to a function directly -- without creating a separate variable of
the union type and assigning the appropriate field of it.

Is gcc being too liberal, or is this behavior simply part of a newer
C-standard, which Sun''s old compiler is not supporting? Thanks!

-mi

解决方案

Mikhail Teterin said:

Hello!

The sample program below is compiled fine by gcc (with -Wall), but
rejected by Sun''s SUNWspro compiler (version 6 update 2).

What sample program?

The point of contention is, whether a value for one of the union''s types
can be passed to a function directly -- without creating a separate
variable of the union type and assigning the appropriate field of it.

The value of the most recently assigned union member object can be passed to
a function directly. There is no need to copy it out.

Is gcc being too liberal, or is this behavior simply part of a newer
C-standard, which Sun''s old compiler is not supporting? Thanks!

Who knows? I don''t see your code.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Richard Heathfield wrote:

What sample program?

Your news-reader must be doing something nasty with attachments.

The program was attached to the original posting. Here it is inline.

-mi

#include <stdio.h>

typedef union {
????????int?????i;
????????void????*p;
????????struct {
????????????????int?????i;
????????????????int?????j;
????????}???????s;
} testunion;

static void
testfunc(testunion u)
{
????????printf("i: %d\np: %p\n", u.i, u.p);
}

int
main()
{
????????testfunc((testunion)3);
????????testfunc((testunion)NULL);
????????return 0;
}


Mikhail Teterin said:

Richard Heathfield wrote:

>What sample program?


Your news-reader must be doing something nasty with attachments.

Attachments don''t happen in comp.lang.c - it''s a text-only newsgroup.

#include <stdio.h>

typedef union {
int i;
void *p;
struct {
int i;
int j;
} s;
} testunion;

static void
testfunc(testunion u)
{
printf("i: %d\np: %p\n", u.i, u.p);

Either you put in an int, in which case it''s okay to print the int but not
the void *, or you put in a void *, in which case it''s okay to print the
void * but not the int.

}

int
main()
{
testfunc((testunion)3);

That''s not a valid conversion.

testfunc((testunion)NULL);

Neither is that.

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于将union的字段传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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