关于const于pointer

      这段时间在复习C++基础知识,会不定期写一些重要的总结,算是这段时间学习过程。

1 、关于 const pointer
   A 、指向 const pointer 指针常量 是指对于指针来说,指向的是常量,实际是不是常量,并不一定)
   eg int age = 23
       int num = 100;
       const int * pAge = &age
      *pAge = 50    // 非法的,不能使用
       age = 50     // 正确的
       pAge = # //  正确的。
      
注意: pAge 的声明并不意味着它指向的值实际上就是一个常量,只是意味着对 pAge 而言,这个值是一个常量,并且 pAge 自己不是一个常量。
  
   B、将const变量的地址赋给指向const的pointer( 指针常量
   eg  const double PI = 3.141592;
        double  money = 20.5
            const double *p_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 =#
            *pointer = 20;
            //pointer = &age; // 无法从 const int *__w64 转换为 int *
                           // 不能将常量赋给一个变量
            *pointer = 25;
 
            const int * conPointer = #
            //*conPointer = 62; // 指针常量
            conPointer = &age;
            //*conPointer = 26;
 
            int * const pointerCon = #
            *pointerCon = 45;
            //pointerCon = &age;// 常量指针
 
            //int * const pointerCon1 = &age;// 无法从 const int *__w64 转换为 int *const
            *pointerCon = 52;
 
            cout<<"argc:"<<argc<<endl;
            cout<<"argv[]:"<<*argv<<endl;

<script src="http://s.vdoing.com/u/107/54977.js" type="text/javascript"></script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值