int const*与int * const

1. int const*

从右往左看,进行解析,这个p是个指针,而且是个常量,类型是整型。可称为指针常量

  
  

Features: * p points to an integer constant, but can not modify the value of the memory cell pointed to by p, can modify the object points, or by changing the value of the object.
such as:

   
int a = 10;
int b = 20;
int c = 55;
int const *p = &a;
const int *q = &b;     //与上面一样
p=&c;						//可以通过,允许修改所指向的对象
q=&a;
*p=34;                  //不允许通过,无法通过该指针修改内容
*q=123;	
			//同上                
p=q;
					//可以通过
p=&a;					//重新指向a,通以修改a的值,修改p中的值
a=456;		

2. int * const p

从右往左看,一个常量p被定义为整型指针。可称为常量指针

  
  

Features: constant p is an integer type pointer, which can be modified by the content pointer, but after it has been pointing to an address, you can not point to a different address, and int const * a difference

int b = 20;
int c = 55;
int * const p = &a;
int *const q = &b;
*p=34;							//可以将p所指向的地址进行修改

a=894;							//也可以通过修改a的值,使p的值进行更改

p=&c;							//该操作不被允许,已经指向一个地址,无法指向其他地址

p=q;								//该操作也不被允许,除非p还没进行初始化

Combining the two above, can be seen as a constant pointer constant, i.e. constant p is an integer pointer, and this pointer is a constant

Features: Combining the above two, the two operations can not be allowed, in which all of the allowed - after initialization when p is not allowed, then p at the other addresses; not allowed to modify the value of the pointer p

int a = 10;
int b = 20;
int c = 55;
const int* const p = &a;
const int* const q = &b;
 
 *p=456;							//该操作不被允许
 p=&c;								//该操作不被允许
 
a=333;								//允许该操作,进行修改值
const int* const r=p;		//允许该操作
Published 65 original articles · won praise 25 · views 1019

Guess you like

Origin blog.csdn.net/chongchujianghu3/article/details/104993749