你如何读取函数返回的两个值? [英] How do you read two values returned by a function?

查看:85
本文介绍了你如何读取函数返回的两个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个返回两个值的函数中,你如何阅读它们?考虑

以下函数:


int addsub(int x,int y)

{int a,b;

a = x + y;

b = xy;

返回(a,b);}


试图读取结果,例如通过g,h = addsub(m,k)只能正确获得

秒值。程序是什么?

In a function that returns two values, how do you read them? Consider
the following function:

int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?

推荐答案

文章< 43 **************** @ news。 lafn.org>,

Clifford Stern< ax *** @ lafn.org>写道:
In article <43****************@news.lafn.org>,
Clifford Stern <ax***@lafn.org> wrote:
在一个返回两个值的函数中,你如何阅读它们?考虑以下函数:

{int a,b;
a = x + y;
b = xy;
返回(a,b);}

试图读取结果,例如通过g,h = addsub(m,k)只得到
第二个值正确。程序是什么?
In a function that returns two values, how do you read them? Consider
the following function:

int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?




addsub(& g,& h);


你可以弄明白其余的。



addsub(&g,&h);

You can figure out the rest.


ax***@lafn.org ( Clifford Stern写道:
ax***@lafn.org (Clifford Stern) writes:
在一个返回两个值的函数中,你如何阅读它们?考虑以下函数:

{int a,b;
a = x + y;
b = xy;
返回(a,b);}

试图读取结果,例如通过g,h = addsub(m,k)只得到
第二个值正确。程序是什么?
In a function that returns two values, how do you read them? Consider
the following function:

int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?




函数不能返回两个值。返回的参数

语句是带括号的逗号表达式;逗号运算符

计算其左操作数(丢弃结果),然后计算其

右操作数,然后产生其右操作数的值。


所以这个:

返回(a,b);

相当于:

返回b;


如果你想从一个函数返回两个值,你可以返回一个

结构,或者你可以通过指针传回这些值。


-

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

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

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



A function cannot return two values. The argument to your return
statement is a parenthesized comma expression; the comma operator
evaluates its left operand (discarding the result), then evaluates its
right operand, then yields the value of its right operand.

So this:
return(a, b);
is equivalent to this:
return b;

If you want to return two values from a function, you can return a
struct, or you can pass the values back via pointers.

--
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.


Keith Thompson< ks *** @ mib.org> schrieb:
Keith Thompson <ks***@mib.org> schrieb:
所以这个:
返回(a,b);
相当于:
返回b;
So this:
return(a, b);
is equivalent to this:
return b;




不是这样。 ''a''得到评估,所以这更等同于:


a;

返回b;


Markus



Not quite so. ''a'' gets evaluated so this would be more equivalent to:

a;
return b;

Markus


这篇关于你如何读取函数返回的两个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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