返回列表 發帖

指標 (四)

本帖最後由 tonyh 於 2014-9-13 17:31 編輯

利用傳統方式, 將變數x與變數y的值對調, 比較與指標法的差異.
指標法:

傳統法:
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x=10, y=5, tmp;
  7.      cout<<"[對調前]"<<endl;
  8.      cout<<"x="<<x<<endl;
  9.      cout<<"y="<<y<<endl;
  10.      cout<<"變數x的位址: "<<&x<<endl;
  11.      cout<<"變數y的位址: "<<&y<<endl<<endl;
  12.      tmp=x;
  13.      x=y;
  14.      y=tmp;
  15.      cout<<"[對調後]"<<endl;
  16.      cout<<"x="<<x<<endl;
  17.      cout<<"y="<<y<<endl;
  18.      cout<<"變數x的位址: "<<&x<<endl;
  19.      cout<<"變數y的位址: "<<&y<<endl<<endl;
  20.      system("pause");
  21.      return 0;
  22. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x = 10, y = 5,tmp;
  7.      
  8.      cout<<"對調前  "<<x<<endl;
  9.      cout<<"x=:  "<<*xPtr<<endl;
  10.      cout<<"y=:  "<<*yPtr<<endl;
  11.      cout<<"變數x的地址"<<xPtr<<endl;
  12.      cout<<"變數y的地址"<<yPtr<<endl;
  13.      tmp = x;
  14.      x=y;
  15.      y=tmp;
  16.       
  17.      cout<<"對調後  "<<x<<endl;
  18.      cout<<"x=:  "<<x<<endl;
  19.      cout<<"y=:  "<<y<<endl;
  20.      cout<<"變數x的地址"<<&x<<endl;
  21.      cout<<"變數y的地址"<<&y<<endl;
  22.      system("pause");
  23.      return 0;
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10,y=5,tmp;
  7.     cout<<"[對調前]"<<endl;
  8.     cout<<"x="<<x<<endl;
  9.     cout<<"y="<<y<<endl;
  10.     cout<<"變數x的位址: "<<&x<<endl;
  11.     cout<<"變數y的位址: "<<&y<<endl<<endl;
  12.     tmp=x;
  13.     x=y;
  14.     y=tmp;
  15.     cout<<"[對調後]"<<endl;
  16.     cout<<"x="<<x<<endl;
  17.     cout<<"y="<<y<<endl;
  18.     cout<<"變數x的位址: "<<&x<<endl;
  19.     cout<<"變數y的位址: "<<&y<<endl<<endl;
  20.     system("pause");   
  21.     return 0;
  22. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;

  4. int main()
  5. {
  6.     int x = 10, y = 5, tmp;
  7.     cout << "[對調前]" << endl;
  8.     cout << " x = :" << x << endl;
  9.     cout << " y = :" << y << endl;
  10.     cout << "變數x的地址 :" << &x << endl;
  11.     cout << "變數y的地址 :" << &y << endl;
  12.     tmp = x;
  13.     x = y;
  14.     y = tmp;
  15.     cout << "[對調後]" << endl;
  16.     cout << " x = :" << x << endl;
  17.     cout << " y = :" << y << endl;
  18.     cout << "變數x的地址 :" << &x << endl;
  19.     cout << "變數y的地址 :" << &y << endl;
  20.     system("pause");
  21.     return 0;
  22. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10, y=5, tmp;
  7.     cout<<"[對調前]"<<endl;
  8.     cout<<"x="<<x<<endl;
  9.     cout<<"y="<<y<<endl;
  10.     cout<<"變數x的位址: "<<&x<<endl;
  11.     cout<<"變數y的位址: "<<&y<<endl;
  12.     tmp=x;
  13.     x=y;
  14.     y=tmp;
  15.     cout<<"[對調後]"<<endl;
  16.     cout<<"x="<<x<<endl;
  17.     cout<<"y="<<y<<endl;
  18.     cout<<"變數x的位址: "<<&y<<endl;
  19.     cout<<"變數y的位址: "<<&x<<endl;
  20.     system("pause");  
  21.     return 0;        
  22. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10, y=5,tmp;
  7.     cout<<"[對調前]"<<endl;
  8.     cout<<"x="<<x<<endl;
  9.     cout<<"y="<<y<<endl;
  10.     cout<<"變數x的位址:"<<&x<<endl;
  11.     cout<<"變數y的位址:"<<&y<<endl;
  12.     tmp=x;
  13.     x=y;
  14.     y=tmp;
  15.     cout<<endl;
  16.     cout<<"[對調後]"<<endl;
  17.     cout<<"x="<<x<<endl;
  18.     cout<<"y="<<y<<endl;
  19.     cout<<"變數x的位址:"<<&x<<endl;
  20.     cout<<"變數y的位址:"<<&y<<endl;
  21.     system("pause");
  22.     return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x=10, y=5, tmp;
  7.      cout<<"[對調前]"<<endl;
  8.      cout<<"x="<<x<<endl;
  9.      cout<<"y="<<y<<endl;
  10.      cout<<"變數x的位址: "<<&x<<endl;
  11.      cout<<"變數y的位址: "<<&y<<endl<<endl;
  12.      tmp=x;
  13.      x=y;
  14.      y=tmp;
  15.      cout<<"[對調後]"<<endl;
  16.      cout<<"x="<<x<<endl;
  17.      cout<<"y="<<y<<endl;
  18.      cout<<"變數x的位址: "<<&x<<endl;
  19.      cout<<"變數y的位址: "<<&y<<endl<<endl;
  20.      system("pause");
  21.      return 0;
  22. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     int x=10,y=5,tmp;
  8.     cout<<"[對調前]"<<endl;
  9.     cout<<"x= "<<x<<endl;
  10.     cout<<"y= "<<y<<endl;
  11.     cout<<"變數x的位址: "<<&x<<endl;
  12.     cout<<"變數y的位址: "<<&y<<endl;
  13.     tmp=x;
  14.     x=y;
  15.     y=tmp;
  16.    cout<<"[對調前]"<<endl;
  17.     cout<<"x= "<<x<<endl;
  18.     cout<<"y= "<<y<<endl;
  19.     cout<<"變數x的位址: "<<&x<<endl;
  20.     cout<<"變數y的位址: "<<&y<<endl;
  21.     system("pause");     
  22.     return 0;   
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x=10,y=5, tmp;
  7.      
  8.       
  9.      
  10.            cout<<"[對調前]"<<endl;
  11.         cout<<"x="<<x<<endl;
  12.     cout<<"y="<<y<<endl;
  13.     cout<<"變數x的位址: "<<&x<<endl;
  14.     cout<<"變數y的位址: "<<&y<<endl<<endl;
  15.     tmp=x;
  16.     x=y;
  17.     y=tmp;
  18.    
  19.     cout<<"[對調後]"<<endl;
  20.     cout<<"x="<<x<<endl;
  21.     cout<<"y="<<y<<endl;
  22.     cout<<"變數x的位址: "<<&x<<endl;
  23.     cout<<"變數y的位址: "<<&y<<endl<<endl;

  24.      
  25.      system("pause");
  26.      return 0;
  27. }
複製代碼

TOP

返回列表