关于const于pointer

来源:互联网 发布:蛋糕西点软件 编辑:程序博客网 时间:2024/05/01 14:43

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

1、关于constpointer
   A、指向constpointer指针常量是指对于指针来说,指向的是常量,实际是不是常量,并不一定)
   egint 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
 
   Cint age = 21;(常量指针指针本身是常量)
      int sloth = 3
      int * const pointer = &age
      *pointer = 100 // 正确
      Pointer = &sloth// 错误
 
   Ddouble 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;

原创粉丝点击