In detail the difference between const int *p and int * const p is of constant pointer and pointer to constant
- 2020-04-02 01:10:53
- OfStack
For Pointers and constants, the following three forms are true:
These three types are described in turn.
Because the * operator is the left operator, the priority of the left operator is from right to left, for
1. Constant Pointers
First look at const and then look at *, p is a pointer of constant type. You cannot change the pointer, but the value stored on the address that the pointer points to can be changed.
Example 1:
Example 2:
2. Pointers to Constants
First look at * then look at const, define a pointer to a constant, can not be used to modify the pointer to the value.
Example 3:
Example 4
So the integer that pointer p points to is a constant whose value cannot be modified.
3. Constant pointer to a constant
For a constant pointer to a constant, you must satisfy both 1 and 2, and you cannot change the value of the pointer or the value of the pointer.
4. Introduce character arrays and character Pointers
Character array and character pointer are defined as follows:
A can be understood as a constant pointer, while p is a pointer to a constant. The code example is as follows:
The output value is:
IaLove You!
I Love You!
a.
L
const char * myPtr = &char_A;//A pointer to a constant
char * const myPtr = &char_A;//Pointer to a constant
const char * const myPtr = &char_A;//A constant pointer to a constant
These three types are described in turn.
Because the * operator is the left operator, the priority of the left operator is from right to left, for
1. Constant Pointers
int * const p
First look at const and then look at *, p is a pointer of constant type. You cannot change the pointer, but the value stored on the address that the pointer points to can be changed.
Example 1:
#include<iostream>
#include<stdlib.h>
using namespace std;
void main()
{
int i1=30;
int i2=40;
int * const pi=&i1;//PI pointer constant here.
//pi=&i2; // Notice here, pi You can't do that anymore, you can't point to another new address. So I've annotated it.
printf("%dn", *pi ) ; //The output is 30
i1=80; //5. Think about it: * PI =80; Instead? Yes, I can change the value of i1 by * PI.
printf("%dn", *pi ) ; //The output is 80
system("pause");
}
Example 2:
char char_A = 'A';
char char_B = 'B';
char * const myPtr = &char_A;
myPtr = &char_B; // error - can't change address of myPtr
2. Pointers to Constants
const int *p
First look at * then look at const, define a pointer to a constant, can not be used to modify the pointer to the value.
Example 3:
#include<iostream>
#include<stdlib.h>
using namespace std;
void main()
{
int i1=30;
int i2=40;
const int * pi=&i1;
printf("%dn", *pi ) ; //The output is 30
pi=&i2; //Note here that PI can reassign a new memory address at any time
i2=80; //Think about it: you can use * PI =80; Instead? Of course not
printf("%dn", *pi ) ; //The output is 80
system("pause");
}
Example 4
char char_A = 'A';
const char * myPtr = &char_A;
*myPtr = 'J'; // error - can't change value of *myPtr
So the integer that pointer p points to is a constant whose value cannot be modified.
3. Constant pointer to a constant
For a constant pointer to a constant, you must satisfy both 1 and 2, and you cannot change the value of the pointer or the value of the pointer.
4. Introduce character arrays and character Pointers
Character array and character pointer are defined as follows:
char a[] = "I Love You!"; //Defines an array of characters
char *p = "I Love You!"; //Defines a character pointer
A can be understood as a constant pointer, while p is a pointer to a constant. The code example is as follows:
#include<iostream>
#include<stdlib.h>
using namespace std;
void main()
{
char a[] = "I Love You!"; //Defines an array of characters, the array name a is a constant pointer to the position of the first element of the array
char *p = "I Love You!"; //Defines a character pointer to a string constant that cannot be modified
//*(p+1)='a';// Error, you cannot change the value the pointer points to, so comment out here.
a[1]='a';//Constant pointer. You cannot change the value of the pointer, but you can change the value that the pointer points to.
//a=p;// Error, a Is a constant pointer whose value cannot be modified.
cout<<a<<endl;
cout<<p<<endl;
cout<<a[1]<<endl;
cout<<*(p+2)<<endl;
system("pause");
}
The output value is:
IaLove You!
I Love You!
a.
L