#include#include //傳址,希望函數幫我改值的狀況就用傳址! int dayBetweenByAddress(int *in, int *out){ int temp; if(in >out){ temp = *in; *in = *out; *out = temp; } return *out-*in; } //傳值 int dayBetweenByValue(int in, int out){ int temp; if(in >out){ temp = in; in = out; out = temp; } return out-in; } int main(){ int day1, day2, dayByAddress, dayByValue; day1 = 10; day2 = 7; dayByAddress = dayBetweenByAddress(&day1, &day2); dayByValue = dayBetweenByValue(day1, day2); printf("-----Call by value-----\n"); printf("%d月到 %d月,共 %d個月\n", day1, day2, dayByValue); printf("-----Call by address---\n"); printf("%d月到 %d月,共 %d個月\n", day1, day2, dayByAddress); }
輸出結果:
沒有留言:
張貼留言