返回列表 發帖

指標 (二)

本帖最後由 tonyh 於 2014-9-13 16:57 編輯

指標變數 --> 專門存放位址的變數
位址運算符號 & --> 取得變數位址
間接運算符號 * --> 取得參考位址內的值

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x=10;
  7.      int *xPtr;  //宣告指標變數 xPtr
  8.      xPtr=&x;    //將變數x的位址 指派給指標變數 xPtr
  9.      //int *xPtr=&x;
  10.      cout<<"變數x的值: "<<x<<endl;
  11.      cout<<"變數x的值: "<<*xPtr<<endl;
  12.      cout<<"變數x的值: "<<*(&x)<<endl;
  13.      cout<<"變數x的位址: "<<&x<<endl;
  14.      cout<<"變數x的位址: "<<xPtr<<endl;
  15.      cout<<"變數x的位址: "<<&(*xPtr)<<endl;
  16.      system("pause");
  17.      return 0;
  18. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10;
  7.     int *xPtr;
  8.     xPtr=&x;
  9.     cout<<"變數x的值: "<<x<<endl;
  10.     cout<<"變數x的值: "<<*xPtr<<endl;
  11.     cout<<"變數x的值: "<<*(&x)<<endl;
  12.     cout<<"變數x的位址: "<<&x<<endl;
  13.     cout<<"變數x的位址: "<<xPtr<<endl;
  14.     cout<<"變數x的位址: "<<&(*xPtr)<<endl;
  15.     system("pause");   
  16.     return 0;
  17. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x = 10;
  7.      int *xPtr;
  8.      xPtr=&x;
  9.      cout<<"變數x的值:  "<<x<<endl;
  10.      cout<<"變數x的值:  "<<*xPtr<<endl;
  11.      cout<<"變數x的值:  "<<*(&x)<<endl;
  12.      cout<<"變數x的地址: "<<&x<<endl;
  13.      cout<<"變數x的地址: "<<xPtr<<endl;
  14.      cout<<"變數x的地址: "<<&(*xPtr)<<endl;
  15.      system("pause");
  16.      return 0;
  17. }
複製代碼

TOP

本帖最後由 張郁庭 於 2014-9-13 17:19 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10;
  7.     int *xPtr;
  8.     xPtr=&x;
  9.     cout<<"變數x的值: "<<x<<endl;
  10.     cout<<"變數x的值: "<<xPtr<<endl;
  11.     cout<<"變數x的值: "<<*(&x)<<endl;
  12.     cout<<"變數x的位置: "<<&x<<endl;
  13.     cout<<"變數x的位置: "<<xPtr<<endl;
  14.     cout<<"變數x的位置: "<<&(*xPtr)<<endl;
  15.     system("pause");
  16.     return 0;  
  17.     }  
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     int x=10;
  8.     int *xPtr=&x;
  9.     cout<<"變數x的數值: "<<x<<endl;
  10.     cout<<"變數x的數值: "<<*xPtr<<endl;
  11.     cout<<"變數x的數值: "<<*(&x)<<endl;
  12.     cout<<"變數x的位址: "<<&x<<endl;
  13.     cout<<"變數x的位址: "<<xPtr<<endl;
  14.     cout<<"變數x的位址: "<<&(xPtr)<<endl;
  15.     system("pause");     
  16.     return 0;   
  17. }
複製代碼

TOP

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

  4. int main()
  5. {
  6.     int x = 10;
  7.     int *xPtr = &x;
  8.     cout << "變數x的值 :" << x << endl;
  9.     cout << "變數x的值 :" << *xPtr << endl;
  10.     cout << "變數x的值 :" << *(&x) << endl;
  11.     cout << "變數x的位置 :" << &x << endl;
  12.     cout << "變數x的位置 :" << xPtr << endl;
  13.     cout << "變數x的位置 :" << &(*xPtr) << endl;
  14.     system("pause");
  15.     return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10;
  7.     int *xPtr;
  8.     xPtr=&x;
  9.     cout<<"變數x的值: "<<x<<endl;
  10.     cout<<"變數x的值: "<<*xPtr<<endl;
  11.     cout<<"變數x的值: "<<*(&x)<<endl;
  12.     cout<<"變數x的位址: "<<&x<<endl;
  13.     cout<<"變數x的位址: "<<xPtr<<endl;
  14.     cout<<"變數x的位址: "<<&(*xPtr)<<endl;
  15.     system("pause");  
  16.     return 0;        
  17. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x=10;
  7.     int *xPtr;
  8.     xPrt=&x;
  9.     int *xPrt=&x;
  10.     cout<<"變數x的值"<<x<<endl;
  11.     cout<<"變數x的值"<<*xPtr<<endl;
  12.     cout<<"變數x的值"<<*(6x)<<endl;
  13.     cout<<"變數x的位址"<<&x<<endl;
  14.     cout<<"變數x的位址"<<xPrt<<endl;
  15.     cout<<"變數x的位址"<<&(*xPrt)<<endl;
  16.     system("pause");
  17.     return 0;
  18. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int x=10;
  7.      int *xPtr;  
  8.      xPtr=&x;   
  9.      
  10.      cout<<"變數x的值: "<<x<<endl;
  11.      cout<<"變數x的值: "<<*xPtr<<endl;
  12.      cout<<"變數x的值: "<<*(&x)<<endl;
  13.      cout<<"變數x的位址: "<<&x<<endl;
  14.      cout<<"變數x的位址: "<<xPtr<<endl;
  15.      cout<<"變數x的位址: "<<&(*xPtr)<<endl;
  16.      
  17.      system("pause");
  18.      return 0;
  19. }
複製代碼

TOP

  1. #include <bits/stdc++.h>

  2. using namespace std;



  3. int main()
  4. {
  5.     int x = 10;
  6.     int *xptr = &x;
  7.     printf("x值:%d, %d, %d\n", x, *xptr, *&x);
  8.     printf("x地址:%p, %p, %p", &x, xptr, *&xptr);
  9.     return 0;
  10. }
複製代碼

TOP

返回列表