include iostream include cstdlib using namespace std void
指標與函數 【範例1】 #include <iostream> #include <cstdlib> using namespace std; void swap(int, int); int main(void) { int a=3, b=5; cout<<"Before swap…n"; cout<<"a="<<a<<“, b="<<b<<endl ; cout<<"After swap…n"; swap(a, b); cout<<"a="<<a<<“, b="<<b<<endl ; system("PAUSE"); return 0; } /* 將兩數互換 */ void swap(int x, int y) { int temp=x; x=y; y=temp; return; } 執行結果: Before swap… a=3, b=5 After swap… a=3, b=5
指標與函數 【範例1-1】 #include <iostream> #include <cstdlib> using namespace std; void swap(int *, int *); int main(void) { int a=3, b=5; cout<<"Before swap…n"; cout<<"a="<<a<<“, b="<<b<<endl ; cout<<"After swap…n"; swap(&a, &b); cout<<"a="<<a<<“, b="<<b<<endl ; system("PAUSE"); return 0; /* 將兩數互換 */ void swap(int *x, int *y) { int temp=*x; *x=*y; *y=temp; return; } 執行結果: Before swap… a=3, b=5 After swap… a=5, b=3
- Slides: 11