当一个函数有多个返回值时,可以使用以下几种方法来解决这个问题:
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;
}
以上是几种常见的解决多个返回值问题的方法,根据具体情况选择合适的方法来解决问题。