Constant (volume) pointers
A constant pointer is a pointer to a constant that we refer to as the name implies.
Pointing to constant variables with constant pointers
In fact, the C + + rule can only use the pointer to the constant variable, the normal pointer to it will be the error, the reason is easy to understand, we use the normal pointer to the constant variable, it is possible to change the value of the constant variable, this is not allowed.
How to define a constant pointer:
Copy Code code as follows:
Const type Name * constant pointer name;
Here's a simple example of how to use it:
Program 1.1.1
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main ()
{
The const int i=5;//defines an int type constant variable
const int *p;//defines an int type constant pointer
p=&i;//with constant pointers to constant variables
cout<<*p<<endl;
return 0;
}
Run Result:
If we use the normal pointer variable to point to the constant variable:
Program 1.1.2
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main ()
{
const int i=5;//defines a constant variable
int *p;//defines an int type pointer
p=&i;//use a generic pointer to a constant variable
cout<<*p<<endl;
return 0;
}
will be an error:
point to a generic variable with a constant pointer (non-const variable)
You can also point to a generic variable with a constant pointer, but you cannot change the value of a generic variable by using a constant pointer, but you can modify the value of a variable by the variable name of a generic variable.
Let's look at an example:
Program 1.2.1
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main ()
{
int i=5;//defines a general variable
The const int *p;//defines a constant pointer
p=&i;//use a constant pointer to a generic variable
cout<<*p<<endl;
i=10;//modifies values by variable names of generic variables
cout<<*p<<endl;
return 0;
}
Run Result:
If we use a constant pointer to modify the value of a generic variable:
Copy Code code as follows:
will be an error:
Summary: A constant pointer can point to both constant and generic variables, but none can change the value of the variable it points to.
Pointer constants
The pointer constant refers to the pointer itself as a constant variable that cannot be changed once the initial value is assigned.
As mentioned above, only regular pointers can point to constant variables, so pointer constants can only point to generic variables, and once the initial value is assigned, it cannot be changed.
To define a method:
Copy Code code as follows:
Type name * Const pointer constant name = variable address;
Because the pointer is a constant variable, it should be assigned an initial value when it is defined.
A program example:
Program 2.1
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main ()
{
int i=5;//defines a general variable
The int * Const p=&i;//defines a pointer constant
cout<<*p<<endl;
*p=30;//to change the value of the variable pointed to by the pointer
cout<<*p<<endl;
return 0;
}
Run Result: