C语言 如何从函数返回两个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6717024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:09:17  来源:igfitidea点击:

How to return two values from a function?

cfunction

提问by Abhineet

Possible Duplicate:
returning multiple values from a function

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

Suppose i have passed two values to a function iCalculate(int x, int y)and this iCalculatereturns 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;
}

回答by Armen Tsirunyan

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

您可以做的另一件事是将数据打包到一个结构中

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;
}

回答by Fred Foo

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 divand ldivdo, except that they call the type differently.)

(这就是标准库的功能divldiv作用,只是它们对类型的调用方式不同。)

回答by dragon135

Your approach is wrong, tempis out of scope/ not longer exist when functon iCalculateexit. 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.

您的方法是错误的,temp超出范围/功能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 定义只是为了将 2 个不相关的值包装在一起太愚蠢了。

回答by ShinTakezou

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]一旦函数返回就会消失,所以调用者有一个“悬空”指针。你必须添加static. 另一种可能更好的方法是让调用者通过它想要存储结果的地方,例如

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.

或类似。

回答by cendar

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);