C语言 一个函数可以返回两个值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5139072/
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
Can a function return two values?
提问by asir
Possible Duplicate:
returning 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?
这里 &ptr->keys[pos].value 将给出地址,Duplicate 是一个设置为 2 的枚举值...如何将这两个值返回到另一个函数,insert_front 是一个链表这个 &ptr->keys[pos] .value 用作链表的第一个地址。我应该编写的调用函数的原型应该是什么?
回答by Ted Hopp
You can't return two values. However, you can return a single value that is a struct that contains two values.
你不能返回两个值。但是,您可以返回单个值,该值是包含两个值的结构体。
回答by Petar Minchev
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.
您只能从函数中返回一件事。要么创建一个包含要返回的所有内容的结构,要么通过引用传递一些函数参数。
回答by Danny Sung
Robert's code is correct. However, you /can/ also use a struct without malloc, like so:
罗伯特的代码是正确的。但是,您 /can/ 也可以使用没有 malloc 的结构,如下所示:
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.
这通常被避免,因为传递这样的结构通常会更昂贵(性能方面),但如果您只需要几个这样的值,它就足够了。
回答by Greg
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:
在 C/C++ 中,一个函数不能返回两个值。然而,大多数情况下,这个限制实际上是有道理的:在任何复杂的函数中,你想要更新一些数据(一堆变量、一个数组等),并返回函数的状态,即它是成功还是不是。因此,一个好的做法是返回函数的状态,并使用引用(在 C++ 中)或指针(在 C 中)来修改你的参数,就像在 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
当然,这个功能太简单了,不会失败(有点),但你明白了:P
And then, if you reallywant your function to return 2 values, then you can use all the tricks proposed by the other answerers.
然后,如果您真的希望您的函数返回 2 个值,那么您可以使用其他回答者提出的所有技巧。
回答by Jerry Coffin
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.
要返回两个值,您必须定义某种结构来保存这两个值,并返回该结构的一个实例。或者,您可以将一两个地址传递给函数,并让它在那里写入值。
回答by Robert S. Barnes
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;
}
回答by david
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.
不,一个函数中不能有两个返回值,第一个返回值将退出创建对象所需的函数。注意:我已经很久没有用 C 编码了,所以这里的语法可能是关闭的。
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);
在创建链接对象之前发布的代码片段中,设置所需的值,然后返回(链接对象);

