Differences between c ++ and C const variables: const Variables
Difference 1:
The const in C Language defines a const variable, which can only be read but not written.
The const of C ++ defines a constant.
Const int a = 5; int array [a]; // It is incorrect in C, because a read-only variable int array [a] is defined in C. // It is correct in c ++ because a constant is defined in C ++.
Difference 2:
Const functions cannot be defined in C language, while const functions can be defined in C ++.
Const member functions of C ++: the value of member variables of the class cannot be modified. (Examples are not listed here)
Pointer constants and constant pointers:
1. Constant pointing to the pointer: When the const is before the * sign, it is the pointer pointing to the constant.
Const char * p1 = "123456"; char const * p2 = "123456"; p1 = "werr"; // correct, you can point to another constant p1 [0] = 'a'; // error. The value pointing to the address cannot be modified.
2. Constant pointer: When the const is after the * sign
Char * const p1 = "12445"; p1 [0] = 'W'; // correct. You can modify the value pointing to the memory p1 = "ddddd"; // error, the memory to which p1 points cannot be modified.