如何从函数中返回两个值? [英] How to we return two values from a function?
问题描述
大家好,
有没有办法从
函数返回两个值............. .......
谢谢,
jayapal
2007年10月29日星期一21:31:49 -0700,jayapal< ja ******** @ gmail.com>
写道:
>大家好,
有没有办法从
函数返回两个值.......... ..........
是和否。
你不能做类似的事情这个:
int double foo(无效)
{
int i = 5;
double d = 10.0;
返回id;
}
但你可以这样做:
< br $>
struct bar1
{
int i;
double d;
};
struct bar1 foo1(void)
{
struct bar1 b;
bi = 5 ;
bd = 10.0;
返回b;
}
/ *或者这个:* /
struct bar2
{
int i;
double d;
};
void foo2(struct bar2 * out_bar2_ptr)
{
out_bar2_ptr-> i = 5;
out_bar2_ptr-> d = 10.0;
}
/ *甚至这个:* /
void foo3(int * i_ptr,double * d_ptr)
{
* i_ptr = 5;
* d_ptr = 10.0;
}
问候
-
jay
jayapal< ja ******** @ gmail.com写的:
#大家好,
#
#有没有办法从
$ b返回两个值$ b#function ....................
(1)返回一个结构。
(2)传入变量地址并将结果分配给它。
-
SM瑞恩 http://www.rawbw.com/~wyrmwif/
降下硫磺就像耐力试验一样,男人。种族灭绝是人们可以参与的最耗费精力的活动。在足球旁边。
jayapal< ja ******** @ gmail.comwrites:
有没有办法从
函数返回两个值.......... ..........
您可以将这两个值放入结构中并返回
结构,或者您可以返回其中一个值是将
指针作为函数的参数之一传递给对象。
如果你能更好地描述一下问题是你想要解决的问题,或许我们可以提供更具体的解决方案。
-
http://footstool.stanford.edu/~blp/private/out.jpeg
Hi all,
Is there any way to return two values from a
function ....................
Thanks,
jayapal
On Mon, 29 Oct 2007 21:31:49 -0700, jayapal <ja********@gmail.com>
wrote:
>Hi all,
Is there any way to return two values from a
function ....................Yes and no.
You can''t do something like this:
int double foo(void)
{
int i = 5;
double d = 10.0;
return i d;
}
But you can do something like this:
struct bar1
{
int i;
double d;
};
struct bar1 foo1(void)
{
struct bar1 b;
b.i = 5;
b.d = 10.0;
return b;
}
/* or this: */
struct bar2
{
int i;
double d;
};
void foo2(struct bar2 *out_bar2_ptr)
{
out_bar2_ptr->i = 5;
out_bar2_ptr->d = 10.0;
}
/* or even this: */
void foo3(int *i_ptr, double *d_ptr)
{
*i_ptr = 5;
*d_ptr = 10.0;
}
Regards
--
jay
jayapal <ja********@gmail.comwrote:
# Hi all,
#
# Is there any way to return two values from a
# function ....................
(1) Return a struct.
(2) Pass in variable addresses and assign the result thereto.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Raining down sulphur is like an endurance trial, man. Genocide is the
most exhausting activity one can engage in. Next to soccer.
jayapal <ja********@gmail.comwrites:
Is there any way to return two values from a
function ....................You could put the two values into a struct and return that
struct, or you could "return" one of the values by passing a
pointer to an object as one of the function''s arguments.
If you can give a better description of the problem that you are
trying to solve, perhaps we can provide a more specific solution.
--
http://footstool.stanford.edu/~blp/private/out.jpeg
这篇关于如何从函数中返回两个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!