The constant pointer to a variable and the pointer to a constant variable are analyzed in detail

  • 2020-04-02 01:51:02
  • OfStack

Constant pointer
A constant pointer is what we call a pointer to a constant, and as the name implies, it's used to point to a constant.

A constant pointer to a constant variable
In fact, C++ stipulates that you can only use a pointer to a constant variable to point to a constant variable. If a normal pointer points to a constant variable, it will report an error. The reason is easy to understand.

Definition method of constant pointer:


const  Type name   *  Often a pointer name ;

Here is a simple example of its use:
Program 1.1.1

#include<iostream>
using namespace std;
int main()
{
 const int i=5;//Defines an int constant
 const int *p;//Defines an int constant pointer
 p=&i;//A constant pointer to a constant variable
 cout<<*p<<endl;
 return 0;
}

Operation results:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130921103243140.png ">

If we use ordinary pointer variables to point to constant variables:
Program 1.1.2

#include<iostream>
using namespace std;
int main()
{
 const int i=5;//Define a constant variable
 int *p;//Defines an int pointer
 p=&i;//Use general Pointers to constant variables
 cout<<*p<<endl;
 return 0;
}

Will report an error:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130921103719953.png ">
Use regular Pointers to general variables (non-const type variables)
Using a regular pointer can also point to a general variable, but you can't change the value of a general variable by a regular pointer, but you can change the value of a variable by its name.
Here's an example:

Program 1.2.1


#include<iostream>
using namespace std;
int main()
{
 int i=5;//Define a general variable
 const int *p;//Defines a constant pointer
 p=&i;//Use regular Pointers to general variables
 cout<<*p<<endl;
 i=10;//Modify the value by the variable name of the general variable
 cout<<*p<<endl; 
 return 0;
}

Operation results:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130921104416593.png ">

If we use a constant pointer to modify the value of a general variable:


*p=10;

Will report an error:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130921104733390.png ">
Summary: regular Pointers can point to both regular and general variables, but neither can change the value of the variable it points to.

Pointer to a constant
Pointer constant refers to the pointer itself is a constant variable, once the initial value can not be changed.
As mentioned above, only constant Pointers can point to constant variables, so pointer constants can only point to general variables, and once given an initial value, cannot be changed.

Definition method:


 Type name  * const  Pointer constant name = Variable address ;

Since a pointer is a constant variable, it should be given an initial value when it is defined.

An example of a program:

Program 2.1


#include<iostream>
using namespace std;
int main()
{
 int i=5;//Define a general variable
 int * const p=&i;//Defines a pointer constant
 cout<<*p<<endl; 
 *p=30;//Use Pointers to change the value of the variable being pointed to
 cout<<*p<<endl;
 return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130921110308937.png ">


Related articles: