Const type variables and Const Type pointer
Const type variable
If you need to use a constant frequently in your program, you can use the compile preprocessing command to define symbolic constants.
Eg: #define PI 3.14159
Const-type variables are also a means of defining constants. A const variable is a special variable that is immutable during a program's operation, as well as other attributes and variables. The const variable must be initialized when it is defined.
eg
const int max=1000; int and const positions can be exchanged.
The difference between a const constant and a symbolic constant:
1) Symbolic constants are processed by the precompiled compiler, and const variables are handled by variables.
2) Symbolic constants are no longer present at compile time, and const constants persist.
3) The scope of the symbolic constant starts at the defined position, ending with the source file, and the const type variable is the same as the general variable.
Const Type pointer
1) Pointer to constant
The keyword const is at the front. It can point to a constant or to a variable, but it cannot modify the amount it points to by it. (The pointer is variable and the content it points to is immutable)
Definition Format: Const < data type > * < pointer variable name >;
eg
const INT * p;
const int a=15;
int b;
p=&a; Legal, p-pointing constant
p=&b; Legal, p-pointing variable
*p=14; Illegal, cannot indirectly modify the amount it points to by P
2) pointer constant
The keyword precedes the pointer variable name. This pointer cannot change the pointer's point, that is, the pointer itself is a constant and does not allow for a re-assignment. Must be initialized at the time of definition. (The pointer is immutable and the content it points to is variable) defines the format:< data type > * Const < pointer variable name >;
eg
int A, B;
int * Const p=&a; Must initialize
*p=15; Legal, you can indirectly access the variable A by P, assigning it a value.
B=*p; Valid, you can remove the value that P points to
p=&b; Illegal, can not give p re-assigned value
3) Pointer constant pointing to constant
The definition format has two const. You cannot change the amount it points to by this pointer , nor can it point to the pointer itself.
eg
int A, B;
const INT * Const p=&a; Initialization
*p=15; It is illegal to change the amount it points to by P.
p=&b; Illegal, can not give p re-assigned value
B=*p; Legal, remove the amount of P point.
a=8; Legal, A is independent, its own operating characteristics are not affected
4) Quickly judge the const pointer--look at the position of the Const.
const int a=18; Constant pointer, const in front of int
int * Const p=&b; Pointer constant, const in front of P
Const-Type and const-type pointers