C++语言指针初探:const int*, int const*,int *const的区别

关于const与指针混合使用的助记法

从右向左读,“*”或者“**”作为分隔,可以分别读作a pointer to 和a pointer to a pointer to;

比如:

const char * p ; // p is a pointer to const char

char const * p; // p is a pointer to char const

规则如下:

* :a pointer to;

**:a pointer to a pointer to;

* const : a const pointer to;

variable: variable name is;

从右至左,以*或**作为分隔,*或**的左边还是按照原来的顺序读下去;

如:

1.  char ** cpp;                             // cpp is a pointer to a pointer to char;

2.  const char ** cpp;                   // cpp is a pointer to a pointer to const  char;

3.  char * const * cpp;                  //cpp is a  pointer to const pointer to  char;

4.  const char * const * cpp;        //cpp is a pointer to const pointer char;

5.  char ** const cpp;                   //cpp is a const pointer to a pointer to char;

6.  char * const * const cpp;        //cpp is a const pointer to a const pointer to char

7.  const char * const * const cpp;    //cpp is a const pointer to const pointer to const char;

1.  char ** cpp;                             // cpp is a pointer toa pointer to char

2.  const char ** cpp;                   // cpp is a pointer toa pointer to const char

3.  char * const * cpp;                  // cpp is a pointer toconst pointer to char

4.  const char * const * cpp;        // cpp is pointer toconst pointer to const char

5.  char ** const cpp;                   // cpp isconst pointer to pointer to char

6.  char * const * const cpp;        // cpp is const pointer to const pointer to char

7.  const char * const * const cpp;    // cpp is const pointer to const pointer to const char



  1 int main(void)
  2 {
  3     const int* ip;//定义了一个指向const int(整型常量) 的指针ip
  4     int const* ip1;定义了一个指向int const(整型常量) 的指针ip2
  5     
  6     const int ci = 0;//定义了一个const int(整型常量)并赋值;
  7     int i = 0;
  8     int j = 0;
  9     
 10     //! int* const ip2;  // error: uninitialized const ‘ip2’//错误:未初始化的const指针;
 11     int* const ip2 = &i; //正确的写法,为const指针初始化;
 12 
 13     //! ip2 = &j;// error: assignment of read-only variable‘ip2’ ip2是个只读指针,不能改变其指向,
 14    //--也就是说,该指针与j的地址空间完全绑定了
 15     ip = &ci;//为ip指针放入ci的地址; 
 16     //! *ip = 1;  // error: assignment of read-only location ‘* ip’//访问ip指针指向的空间内的变量,但这个变量空间是只读的!
 17 
 18     ip1 = &ci;//为ip1指针放入ci的地址;
 19     //! *ip1 = 1; // error: assignment of read-only location ‘* ip1’同上
 20 
 21     return 0;
 22 }   

以上代码和知识来源于skywalker_leo的总结,后辈再次消化吸收和重新加入自己的理解;

const int和int const辨析:

其实两者只是从语法上不同,其实都是表示的是整型常量,也可以理解为只读的整型变量;

int *const ptr辨析;

这个可以看以上的11和13行,11行为只读指针(或者不可修改指针、绑定空间指针)放入地址空间的地址,之后就永远地和该地址空间绑定了,也就是说这个指针本身是不可修改的,是只读的,因为指针只能放入地址,指向某一变量空间的地址是不能被改变的,所以,之后的13行再向指针里面放入另一个变量空间的地址,那么就会报错,注意这里的错误是read-only variable,variable是变量的意思,是指的是指针变量空间是只读的,不同于之后的location,location是空间、地方的意思,这里指的是*ip,是指针变量指向的变量空间的位置,但这个空间是const int,这个空间是只读的,所以报错;

         int j = 0;
	 int * const i = &j;
	 (*i)++;
这里的i是个不可修改的、只读指针,所以指针变量地址空间里所存放的地址值(也就是j变量的地址)是不可修改的,但是我们却可以通过该指针对j变量空间进行访问,改变j空间存放的值,这样就非常便于理解了;

猜你喜欢

转载自blog.csdn.net/sinat_26394043/article/details/80054199