开发者

How to return two values from a function? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

returning multiple values from a function

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

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


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


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


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.

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.


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.

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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜