开发者

Can a function return two values? [duplicate]

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

Possible Duplicate:

returning开发者_StackOverflow中文版 multiple values from a function

I have a function which has to return two values

                    return(&ptr->keys[pos].value)
                    return Duplicate;

Here &ptr->keys[pos].value will give the address and Duplicate is a enum value set to 2... How to return these two values to another function, insert_front which is a linked list this &ptr->keys[pos].value is used as the first address for linked list. What should be the prototype of the calling function I should write?


You can't return two values. However, you can return a single value that is a struct that contains two values.


You can return only one thing from a function. Either you make a struct which contains all the things you want to return, or you pass some function parameters by reference.


Robert's code is correct. However, you /can/ also use a struct without malloc, like so:

struct retstruct {
    int a;
    int b;
};
struct retstruct myfunc(void) {
    struct retstruct r;
    r.a = 1;
    r.b = 2;
    return r;
}

int main()
{
    struct retstruct r;
    r = myfunc();
    printf("a=%d  b=%d\n", r.a, r.b);
}

This is typically shunned because it's generally a bit more costly (performance-wise) to be passing around structs like this, but if you only need a couple values like this, it can be sufficient.


To return two values, you'll have to define some sort of struct to hold both values, and return an instance of that struct. Alternatively, you can pass an address or two to the function, and have it write the value(s) there.


No, you can not have two returns in a function, the first return will exit the function you will need to create an object. Note: I have not coded in C in a long time, so the syntax here is probably off.

public class link{
  private int& ptr;
  private int Duplicate;

  public constructor link() {}

  public setPtr(int& ptr) {
     this->&ptr = ptr;
  }

  //set duplicate function

  //get ptr function

  //get duplicate function

}

and in the function you snippet you posted before create a link object set the values desired, and then return(link object);


In C/C++, a function cannot return two values. However, most often, this limitation actually makes sense: in any complex function, you want to update some data (a bunch of variables, an array, etc.), and return the status of the function, i.e., whether it was successful or not. As a result, a good practice is to return the status of the function, and use references (in C++) or pointers (in C) to modify your arguments, just like in this example in C:

int swap(int* a, int* b)
{
   int tmp = *a;
   *a = *b;
   *b = tmp;
   return 0;
}

Of course, this function is too simple to fail (sort of), but you get the idea :P

And then, if you really want your function to return 2 values, then you can use all the tricks proposed by the other answerers.


Like this:

typedef ret {
    int val1;
    int *val2;
} ret_t ;

// make sure to remember to free the struct when you're done with it
ret_t *myFunc( void ) {
    ret_t *r = malloc( sizeof(ret_t) );
    r->val1 = 1;
    r->val2 = &r.val1;

    return r;
}

// You can also return a copy of the struct on the stack
ret_t myFunc( void ) {
    ret_t r;
    r.val1 = 1;
    r.val2 = &r.val1;

    return r;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜