如何从一个函数返回两个值?(复制)

时间:2021-05-17 02:01:13

Possible Duplicate:
returning multiple values from a function

可能的重复:从函数返回多个值

Suppose i have passsed two values to a function iCalculate(int x, int y) and this iCalculate returns two values. Those are as follows:-

假设我将两个值传递给一个函数iCalculate(int x, int y),这个iCalculate返回两个值。这些如下:-

  • (x*y)
  • (x * y)
  • (x/y)
  • (x / y)

Now how should i return the above two values at the same time with the same function?

现在,我该如何用相同的函数同时返回上述两个值呢?

My Approach was:-

我的方法是:-

int* iCalculate(int x,int y){
   int temp[2];
   temp[0] = x*y;
   temp[1] = x/y;
   return temp;
}

5 个解决方案

#1


28  

returning the address of the first element of a local array has undefined behavior(at least dereferencing it later is).

返回本地数组的第一个元素的地址具有未定义的行为(至少在以后取消它的引用)。

You may use output parameters, that is, pass two pointers, and set the values inside

您可以使用输出参数,即传递两个指针,并在其中设置值

void Calculate(int x, int y, int* prod, int* quot)
{
    *prod = x*y;
    *quot = x/y;
}

usage:

用法:

int x = 10,y = 2, prod, quot;
Calculate(x, y, &prod, &quot)

Another thing you could do is pack your data into a struct

另一种方法是将数据打包到一个struct中

typedef struct 
{
    int prod;
    int quot;
} product_and_quot;


product_and_quot Calculate(int x, int y)
{
    product_and_quot p = {x*y, x/y};
    return p;
}

#2


11  

That won't work, since you're returning a pointer to a temporary array, which will stop existing at function return time.

这将不起作用,因为您正在返回一个指向临时数组的指针,该数组将在函数返回时停止存在。

Instead, define

相反,定义

typedef struct { int first, second; } IntPair;

and return an object of that type.

返回该类型的对象。

(This is what the standard library functions div and ldiv do, except that they call the type differently.)

(这就是标准库函数div和ldiv所做的事情,只不过它们的类型不同。)

#3


2  

Your approach is wrong, temp is out of scope/ not longer exist when functon iCalculate exit. So you must not return the address of temp. That would be address of out of scope/ no longer exist variable. Accessing that address means undefined behaviour.

您的方法是错误的,当函数iCalculate退出时,温度超出范围/不再存在。因此,您不能返回temp的地址。那将是超出范围的地址/不再存在的变量。访问该地址意味着未定义的行为。

You can use this approach:

你可以使用这个方法:

void iCalculate(int x,int y,int *mult,int *divi){
   *mult = x*y;
   *divi = x/y;
}

or you can use another approach:

或者你可以使用另一种方法:

typedef struct{ 
   int mul, divi;
} TResult;

TResult iCalculate(int x,int y){
   TResult res;
   res.mul = x*y;
   res.divi = x/y;
   return res;
}

or :

或者:

void iCalculate(int x,int y,TResult *res){
   res->mul = x*y;
   res->divi = x/y;
}

I suggest the first approach. I think it is too silly to create a new struct definition only to wrap 2 unrelated value together.

我建议第一种方法。我认为创建一个新的struct定义仅仅将两个不相关的值打包在一起是太愚蠢了。

#4


1  

The way you did is wrong since int temp[2] disappears once the function returns, so the caller has a "dangling" pointer. You have to add static. Another way, likely better, is to let the caller pass where it wants the result be store e.g.

这样做是错误的,因为int temp[2]在函数返回时消失,所以调用者有一个“悬浮”指针。你必须添加静态。另一种可能更好的方法是让呼叫者通过它想要的地方。

void iCalc(int x, int y, int *rp, int *rq)
{
   // check if rp and rq are NULL, if so, returns
   *rp = x*y;
   *rq = x/y; // y != 0, and this will truncate of course.
}

and the caller will do something like

打电话的人会做一些类似的事情。

int res[2];
iCalc(x, y, res, res+1);

or similar.

或类似的。

#5


1  

Your approach was not so wrong you can return the address of the table like this :

你的方法没有错,你可以这样返回表格的地址:

int *iCalculate(int x,int y){
    int *temp=malloc(sizeof(int)*2);
    temp[0]=x*y;
    temp[1]=x/y;
    return temp;
}

dont forget to free the memory :

不要忘记释放记忆:

int *result;
result=iCalculate(10,7);
printf("%d %d\n",result[0],result[1]);
free(result);

#1


28  

returning the address of the first element of a local array has undefined behavior(at least dereferencing it later is).

返回本地数组的第一个元素的地址具有未定义的行为(至少在以后取消它的引用)。

You may use output parameters, that is, pass two pointers, and set the values inside

您可以使用输出参数,即传递两个指针,并在其中设置值

void Calculate(int x, int y, int* prod, int* quot)
{
    *prod = x*y;
    *quot = x/y;
}

usage:

用法:

int x = 10,y = 2, prod, quot;
Calculate(x, y, &prod, &quot)

Another thing you could do is pack your data into a struct

另一种方法是将数据打包到一个struct中

typedef struct 
{
    int prod;
    int quot;
} product_and_quot;


product_and_quot Calculate(int x, int y)
{
    product_and_quot p = {x*y, x/y};
    return p;
}

#2


11  

That won't work, since you're returning a pointer to a temporary array, which will stop existing at function return time.

这将不起作用,因为您正在返回一个指向临时数组的指针,该数组将在函数返回时停止存在。

Instead, define

相反,定义

typedef struct { int first, second; } IntPair;

and return an object of that type.

返回该类型的对象。

(This is what the standard library functions div and ldiv do, except that they call the type differently.)

(这就是标准库函数div和ldiv所做的事情,只不过它们的类型不同。)

#3


2  

Your approach is wrong, temp is out of scope/ not longer exist when functon iCalculate exit. So you must not return the address of temp. That would be address of out of scope/ no longer exist variable. Accessing that address means undefined behaviour.

您的方法是错误的,当函数iCalculate退出时,温度超出范围/不再存在。因此,您不能返回temp的地址。那将是超出范围的地址/不再存在的变量。访问该地址意味着未定义的行为。

You can use this approach:

你可以使用这个方法:

void iCalculate(int x,int y,int *mult,int *divi){
   *mult = x*y;
   *divi = x/y;
}

or you can use another approach:

或者你可以使用另一种方法:

typedef struct{ 
   int mul, divi;
} TResult;

TResult iCalculate(int x,int y){
   TResult res;
   res.mul = x*y;
   res.divi = x/y;
   return res;
}

or :

或者:

void iCalculate(int x,int y,TResult *res){
   res->mul = x*y;
   res->divi = x/y;
}

I suggest the first approach. I think it is too silly to create a new struct definition only to wrap 2 unrelated value together.

我建议第一种方法。我认为创建一个新的struct定义仅仅将两个不相关的值打包在一起是太愚蠢了。

#4


1  

The way you did is wrong since int temp[2] disappears once the function returns, so the caller has a "dangling" pointer. You have to add static. Another way, likely better, is to let the caller pass where it wants the result be store e.g.

这样做是错误的,因为int temp[2]在函数返回时消失,所以调用者有一个“悬浮”指针。你必须添加静态。另一种可能更好的方法是让呼叫者通过它想要的地方。

void iCalc(int x, int y, int *rp, int *rq)
{
   // check if rp and rq are NULL, if so, returns
   *rp = x*y;
   *rq = x/y; // y != 0, and this will truncate of course.
}

and the caller will do something like

打电话的人会做一些类似的事情。

int res[2];
iCalc(x, y, res, res+1);

or similar.

或类似的。

#5


1  

Your approach was not so wrong you can return the address of the table like this :

你的方法没有错,你可以这样返回表格的地址:

int *iCalculate(int x,int y){
    int *temp=malloc(sizeof(int)*2);
    temp[0]=x*y;
    temp[1]=x/y;
    return temp;
}

dont forget to free the memory :

不要忘记释放记忆:

int *result;
result=iCalculate(10,7);
printf("%d %d\n",result[0],result[1]);
free(result);