使用union作为函数参数 [英] Using a union as a function parameter

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

问题描述

当我编译代码(下面)时,我收到此错误:

无法将参数1从''int''转换为''union dna''


不用说:


FOO x;

x.val = 100;


.. ..可以使用union作为函数参数,并且当调用该函数时,将参数作为

union的类型之一传递(int,在下面的例子中)?

typedef union foo

{

void * ptr;

int val; < br $>
} FOO;


void * test(FOO obj)

{

void * ptr = obj.ptr;

int val = obj.val;

返回NULL;

}


int main(int argc,char * argv [])

{

int x = 100;

test(x);

返回0;

}

解决方案

Juke All< me @ here达网络>写在

新闻:ai ******************************** @ 4ax.com:

当我编译代码(下面)时,我收到此错误:
无法将参数1从''int''转换为''union dna''
不用说:

FOO x;
x.val = 100;

...是否可以使用union作为函数参数,当调用该函数时,将参数作为
union的类型之一传递(int,在下列情况下)?

typedef union foo
{
void * ptr;
int val;
} FOO;

void * test(FOO obj)

ptr = obj.ptr;
int val = obj.val;
返回NULL;
}
int main(int argc,char * argv [])
{
int x = 100;
test(x);




^ *& ^&你在想吗?你告诉编译器test()

需要一个FOO作为参数然后你给它一个臭臭的int?什么部分

的编译器错误是不是很清楚?尝试将''''声明为类型FOO并再次尝试




-

- 马克 - >

-


2004年5月27日21:05:15 GMT,Mark A. Odell < od ******* @ hotmail.com>

写道:

Juke All< me@here.net>在
新闻中写道:ai ******************************** @ 4ax.com:

当我编译代码(下面)时,我收到此错误:
无法将参数1从''int''转换为''union dna''
不用说:

FOO x;
x.val = 100;

...是否可以使用union作为函数参数,当
调用该函数,将参数作为
union的类型之一传递(int,在下列情况下)?

typedef union foo
{
void * ptr;
int val;
} FOO;

void * test(FOO obj)
{
void * ptr = obj。 ptr;
int val = obj.val;
返回NULL;
}
int main(int argc,char * argv [])
{
int x = 100;
test(x);



^ *& ^&你在想吗?你告诉编译器test()
将FOO作为参数然后你给它一个臭臭的int?编译器错误的哪一部分不清楚?尝试将x声明为类型FOO并再次尝试


-
- 标记 - >



我知道这是有效的,因此,我的信息的一部分,我说:


不说:

FOO x;

x .val = 100;


我只是想知道是否有可能将一个int传递给

,它们需要union foo(以及一个工会成员)是一个int)。


Juke All< me@here.net>写在

新闻:tc ******************************** @ 4ax.com:

void * test(FOO obj)
{
void * ptr = obj。 ptr;
int val = obj.val;
返回NULL;
}
int main(int argc,char * argv [])
{
int x = 100;
test(x);
^ *& ^&你在想吗?你告诉编译器test()
将FOO作为参数然后你给它一个臭臭的int?什么
部分编译器错误不清楚?尝试将''''声明为类型FOO
然后再试一次。



我知道这是有效的,因此,我的消息部分我说:

不用说:
FOO x;
x.val = 100;

我只是想知道是否可以将int传递给某些东西
期望联盟foo(其中一个联盟成员是一个int)。




当然不是!这就是打字语言的重点。如果你想要
保存一些打字,你可以在定义点初始化x,比如

这个:


int main(无效)

{

FOO x = {NULL,100};

test(x);


返回0;

}


这可能不会做你想要的。另外,意识到你正在通过

a副本x进入测试,这很好,但如果你希望test()修改x

供main()使用,你需要改变测试()取代FOO *。


-

- 马克 - >

-


When I compile the code (below), I get this error:
cannot convert parameter 1 from ''int'' to ''union dna''

Without saying:

FOO x;
x.val = 100;

....is it possible to use a union as a function parameter, and when
calling that function, pass the argument as one of the types of the
union (int, in the following case)?
typedef union foo
{
void* ptr;
int val;
} FOO;

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);
return 0;
}

解决方案

Juke All <me@here.net> wrote in
news:ai********************************@4ax.com:

When I compile the code (below), I get this error:
cannot convert parameter 1 from ''int'' to ''union dna''

Without saying:

FOO x;
x.val = 100;

...is it possible to use a union as a function parameter, and when
calling that function, pass the argument as one of the types of the
union (int, in the following case)?
typedef union foo
{
void* ptr;
int val;
} FOO;

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);



What the ^*&^& are you thinking here? You told the compiler that test()
takes a FOO as a parameter and then you give it a stinking int? What part
of the compiler error isn''t clear? Try declaring ''x'' as type FOO and try
again.

--
- Mark ->
--


On 27 May 2004 21:05:15 GMT, "Mark A. Odell" <od*******@hotmail.com>
wrote:

Juke All <me@here.net> wrote in
news:ai********************************@4ax.com :

When I compile the code (below), I get this error:
cannot convert parameter 1 from ''int'' to ''union dna''

Without saying:

FOO x;
x.val = 100;

...is it possible to use a union as a function parameter, and when
calling that function, pass the argument as one of the types of the
union (int, in the following case)?
typedef union foo
{
void* ptr;
int val;
} FOO;

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);



What the ^*&^& are you thinking here? You told the compiler that test()
takes a FOO as a parameter and then you give it a stinking int? What part
of the compiler error isn''t clear? Try declaring ''x'' as type FOO and try
again.

--
- Mark ->


I know that works, hence, the part of my message where I said:

Without saying:
FOO x;
x.val = 100;

I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).


Juke All <me@here.net> wrote in
news:tc********************************@4ax.com:

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);
What the ^*&^& are you thinking here? You told the compiler that test()
takes a FOO as a parameter and then you give it a stinking int? What
part of the compiler error isn''t clear? Try declaring ''x'' as type FOO
and try again.


I know that works, hence, the part of my message where I said:

Without saying:
FOO x;
x.val = 100;

I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).



Of course not! That''s the whole point of a typed language. If you want to
save some typing you can initialize x at the point of definition like
this:

int main(void)
{
FOO x = { NULL, 100 };
test(x);

return 0;
}

This probably won''t do what you want though. Also, realize you are passing
a copy of x into test which is fine but if you expect test() to modify x
for use by main() you will need to change test() to take a FOO * instead.

--
- Mark ->
--


这篇关于使用union作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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