當一個函數有多個返回值時,可以使用以下幾種方法來解決這個問題:
void func(int a, int b, int* sum, int* difference) {
*sum = a + b;
*difference = a - b;
}
int main() {
int a = 10, b = 5, sum, difference;
func(a, b, &sum, &difference);
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
return 0;
}
typedef struct {
int sum;
int difference;
} Result;
Result func(int a, int b) {
Result result;
result.sum = a + b;
result.difference = a - b;
return result;
}
int main() {
int a = 10, b = 5;
Result result = func(a, b);
printf("Sum: %d\n", result.sum);
printf("Difference: %d\n", result.difference);
return 0;
}
int sum;
int difference;
void func(int a, int b) {
sum = a + b;
difference = a - b;
}
int main() {
int a = 10, b = 5;
func(a, b);
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
return 0;
}
以上是幾種常見的解決多個返回值問題的方法,根據具體情況選擇合適的方法來解決問題。