函數無返回值,三個浮點數參數,實現:
1. 求最大值、最小值。
2. 交換最大值與最小值。
3. 打印三數。
float a,b,c(值傳遞),函數內修改不影響外部。&(a>b?...) 非法(臨時值無地址)。#include <stdio.h>
void MaxMinChangeEachOtherNew(float *a, float *b, float *c) {
float *max = a, *min = a;
if (*b > *max) max = b;
if (*c > *max) max = c;
if (*b < *min) min = b;
if (*c < *min) min = c;
float tmp = *max;
*max = *min;
*min = tmp;
}
int main() {
float a = 1.2f, b = 3.0f, c = 2.1f;
printf("交換前: %.1f %.1f %.1f\n", a, b, c);
MaxMinChangeEachOtherNew(&a, &b, &c);
printf("交換後: %.1f %.1f %.1f\n", a, b, c);
return 0;
}
交換前: 1.2 3.0 2.1
交換後: 3.0 1.2 2.1
*p 大小,移動指針指向目標變數。*max、*min 直接操作原變數。&(cond?x:y) —— 結果是右值,無地址。NULL,函數開頭加判空保護。掌握「傳指針、比解引用、換值」,C 語言函數修改多變數不再是難題!