Board logo

標題: 指標 (四) [打印本頁]

作者: tonyh    時間: 2014-9-13 17:24     標題: 指標 (四)

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

利用傳統方式, 將變數x與變數y的值對調, 比較與指標法的差異.
指標法:
[attach]968[/attach]
傳統法:
[attach]969[/attach]
  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. }
複製代碼

作者: 林宇翔    時間: 2014-9-13 17:30

  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. }
複製代碼

作者: 張峻瑋    時間: 2014-9-13 17:30

  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. }
複製代碼

作者: 李允軒    時間: 2014-9-13 17:31

  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. }
複製代碼

作者: 周雍程    時間: 2014-9-13 17:32

  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. }
複製代碼

作者: 劉得旗    時間: 2014-9-13 17:33

  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. }
複製代碼

作者: 張郁庭    時間: 2014-9-13 17:34

  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. }
複製代碼

作者: 張彥承    時間: 2014-9-13 17:36

  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. }
複製代碼

作者: 劉得恩    時間: 2014-9-17 13:14

  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. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2