關於const於pointer

來源:互聯網
上載者:User

      這段時間在複習C++基礎知識,會不定期寫一些重要的總結,算是這段時間學習過程。

1、關於const與pointer   A、指向const的pointer( 指標常量—是指對於指標來說,指向的是常量,實際是不是常量,並不一定)   eg:int age = 23;       int num = 100;       const int * pAge = &age;      *pAge = 50;    // 非法的,不能使用       age = 50;     // 正確的       pAge = &num; // 正確的。       注意: pAge 的聲明並不意味著它指向的值實際上就是一個常量,只是意味著對 pAge 而言,這個值是一個常量,並且 pAge 自己不是一個常量。       B、將const變數的地址賦給指向const的pointer( 指標常量)   eg: const double PI = 3.141592;        double  money = 20.5;            const double *p_PI = &PI;        p_PI = &money;// OK    C、int age = 21;( 常量指標—指標本身是常量)      int sloth = 3;      int * const pointer = &age;      *pointer = 100; // 正確      Pointer = &sloth;// 錯誤    D、double trouble = 2.65; ( 指標常量指標)       const double * const stick = &trouble;       *stick = 3.62; // 錯誤       Stick = &money; // 錯誤             int num = 15;            const int age = 22;             int * pointer =&num;            *pointer = 20;            //pointer = &age; //無法從“const int *__w64 ”轉換為“int *”                           //不能將常量賦給一個變數            *pointer = 25;             const int * conPointer = &num;            //*conPointer = 62; //指標常量            conPointer = &age;            //*conPointer = 26;             int * const pointerCon = &num;            *pointerCon = 45;            //pointerCon = &age;//常量指標             //int * const pointerCon1 = &age;//無法從“const int *__w64”轉換為“int *const”            *pointerCon = 52;             cout<<"argc:"<<argc<<endl;            cout<<"argv[]:"<<*argv<<endl;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.