C - Returning 2 values in a function and trying to use them
c
Solution
No, you cannot do this in C, you can just return one value. The comma operator returns just the last value, so you are actually returning the second one.
You can pass data by reference, like
function(&a, &b);
void function(int *a, int* b){
*a = 42;
*b = 666;
}
You can also put them in a structure and return it or even a pointer to a dynamically allocated structure.
typedef struct int_pair {
int a, b;
} int_pair;
int_pair function(){
int_pair s;
s.a = 42;
s.b = 666;
return s;
}
Problem
So I have this function where I return 2 values: ``` return result, result2; ``` Now what I am trying to do is use these 2 values in my main function. I want to save it into a variable in main function like this : ``` variable = function(a,b); ``` But how do you specify here which value you want?