1.const Basic Knowledge
(1) Defining constants with const
const int a = 5; Defines an int type constant A with a value of 5
(Note: It is stored in the symbol table (Key,value) in C + + and does not allocate memory space, only allocates memory when the address is taken or is defined as a global variable for use in other files)
(2) const and pointer
1 Const int // P1 is a pointer to constant a, the value of a cannot be changed by the pointer P1 2 3 int Const P2 =&b; // P2 is a pointer to B only, and the value of B can be changed by P2 4 Const int Const P3 =&c; // P3 is a constant pointer to C only, and the value of C cannot be changed by the pointer P3!
(3) const and reference
Weekend repair ...
The similarities and differences of 2.const in C + + and C
In C is "hypocrite", "counterfeit" ...
1 void main () 2 3 const int a = 5 ; 4 int *p = &A; 5 *p = 6 printf ( " a =%d " ,a); 7 }
In the above code we use the const definition of the constant A, in the C language can be pointed to a pointer p to modify the content of a, this is inconceivable, so we say that the Const in C is "hypocrite", "counterfeit", and did not play his role. But
In C + + is a veritable.
The reason is also mentioned above, that is, the constants defined in C + + are stored in the symbol table (key,value), do not allocate memory space, only when the address or defined as a global variable is used in other files to allocate memory space, we use constant constant name to access the constant when using only the constants in the symbol table, Changes to the constant values in the memory space do not affect the values stored in the symbol table.
The similarities and differences of 3.constyu#define macro definition
Weekend Supplement ...
The similarities and differences between const basic knowledge, const in C + + and C, and the definition of const and # define macros