const int * int * const difference between the

(This is after a long time began to write, I'm sorry to say my opening blog)
pointer constant pointer pointing constant

int const* p  //指向常量的指针
const int* p  //指向常量的指针
int *const p  //常量指针

Three or more language is C, less commonly used, but when learned C ++, it is confusing, and first const int * p int const * p are equivalent, denotes the value of p can not be changed, but p (address) can be changed.
(1) a constant pointer int * const p

#include <stdio.h>
#include <stdlib.h>
int main()
{
int p1=10;
int* const p=&p1;
printf("%d ",*p);  //此时输出*p的值是10
*p=20;
printf("%d ",p1);  //此时输出p1是20,因为通过*p修改了p1的值
system("pause");
return 0;
}

That can not modify the position of the pointer points, but the value of the pointer position can be modified.
(2) a constant pointer pointing const int * p

#include <stdio.h>
#include <stdlib.h>
int main()
{
int p1=10;
int p2=20;
const int* p=&p1;
printf("%d ",*p);  //输出10
/* *p=40;
printf("%d ",p1) */ //不能修改指针指向的地址来改变指针指向的值,输出会报错,因此注释掉
p=&p2;
p2=30;
printf("%d ",*p);  //输出30
system("pause");
return 0;
}

Not change this pointer points to the address by modifying the value of
(3) constant const pointer
to const pointer constant, can substantially modify the position of the pointer is pointing can not modify the value of pointer

Today's share ended

Published 33 original articles · won praise 13 · views 1070

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/102327173