I don't know what your string is, but I'm going to assume that it manages its own memory.
You have two solutions:
1: Return a struct which contains all the types you need.
struct Tuple {
int a;
string b;
};
struct Tuple getPair() {
Tuple r = { 1, getString() };
return r;
}
void foo() {
struct Tuple t = getPair();
}
2: Use pointers to pass out values.
void getPair(int* a, string* b) {
// Check that these are not pointing to NULL
assert(a);
assert(b);
*a = 1;
*b = getString();
}
void foo() {
int a, b;
getPair(&a, &b);
}
Which one you choose to use depends largely on personal preference as to whatever semantics you like more.
Answer from Travis Gockel on Stack OverflowI don't know what your string is, but I'm going to assume that it manages its own memory.
You have two solutions:
1: Return a struct which contains all the types you need.
struct Tuple {
int a;
string b;
};
struct Tuple getPair() {
Tuple r = { 1, getString() };
return r;
}
void foo() {
struct Tuple t = getPair();
}
2: Use pointers to pass out values.
void getPair(int* a, string* b) {
// Check that these are not pointing to NULL
assert(a);
assert(b);
*a = 1;
*b = getString();
}
void foo() {
int a, b;
getPair(&a, &b);
}
Which one you choose to use depends largely on personal preference as to whatever semantics you like more.
Option 1: Declare a struct with an int and string and return a struct variable.
struct foo {
int bar1;
char bar2[MAX];
};
struct foo fun() {
struct foo fooObj;
...
return fooObj;
}
Option 2: You can pass one of the two via pointer and make changes to the actual parameter through the pointer and return the other as usual:
int fun(char **param) {
int bar;
...
strcpy(*param,"....");
return bar;
}
or
char* fun(int *param) {
char *str = /* malloc suitably.*/
...
strcpy(str,"....");
*param = /* some value */
return str;
}
Option 3: Similar to the option 2. You can pass both via pointer and return nothing from the function:
void fun(char **param1,int *param2) {
strcpy(*param1,"....");
*param2 = /* some calculated value */
}
Videos
Can I use arrays to return multiple values from a function in C?
How can I return multiple values from a function in C using pointers?
What are the best practices for returning multiple values from a function in C?
https://www.fluentcpp.com/2021/07/09/how-to-return-several-values-from-a-function-in-c/
Nice discussion of the pros/cons of various ways of returning multiple values from a function.
Wow. This is a spectacularly misleading result.
No, you can not return multiple values like this in C. A function can have at most one single return value. You can't use a comma to return multiple values like this.
But, if you can't do it, then why did it seem to almost work? That's a rather complicated question, that it probably won't be possible to completely answer here.
Your program has two, completely unrelated problems. The first problem, which isn't too serious, is the line
return a, b, c;
at the end of function sum. In English, this line basically says, "Take the value a and throw it away, then take the value b and throw it away, then take the value c and have it be the actual return value." (See What does the comma operator do? for more about this comma operator.)
And then the second, much more serious problem is the line
printf("%d %d %d", x);
in main. Your compiler should have warned you about this problem; mine says warning: more '%' conversions than data arguments. You're telling printf, "I'm going to give you three int values to print". But you lied, because you're then giving it just one int value, x. So printf is blindly reaching for the other two values you promised, and getting... what?
By a reasonably incredible coincidence, when printf reaches out to fetch the values you didn't pass, it is somehow grabbing... one of the values that got thrown away by the poorly-written return statement back in function sum. I'm not even sure how this happened, although when I tried your program on my computer, I got a similar result.
This may be too obvious to mention, but: this result is not guaranteed, it's not something to depend on, and it's not even something to learn from. If you want to return multiple values from a function, there are other ways to do that, as this question's other answers have suggested.
Short answer: no, you cannot return multiple values (at least, not as you did). Good news, you do have some ways to return multiple values from a function:
Method 1: use a struct
typedef struct result {
int a;
int b;
int c;
} result_t;
result_t foo(int a, int b, int c) {
return (result_t) { a + 1, b + 1, c + 1 };
}
int main(void) {
result_t res = foo(1, 2, 3);
printf("res.a is %d\n", res.a); // OUTPUT: res.a is 2
return 0;
}
Method 2: use pointers as function arguments
void some_calc(int input, int* output1, int* output2) {
*output1 = input * 2;
*output2 = input / 2;
}
int main(void) {
int res1, res2;
some_calc(50, &res1, &res2);
printf("50 * 2 = %d and 50 / 2 = %d\n", res1, res2);
return 0;
}
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.