Four situations in which const and pointers are combined
The four cases were as follows:
The int cons *p p itself is not a constant, and the data for the address that P points to is constant
the const int *P p itself is not a constant, and the data for the address pointed to by P is constant
int * Const P p is constant, p is not constant in the address space pointed to
the const int * Const P p is a constant, and the address space pointed to by P is also a constant
Summary: When the const is on the left side, the data of the address that the P points to is modified, and when the const is on the right, the modified P
When a variable is modified with a const, the value of the variable can still be modified by the pointer under GCC
int main (void)
{
const int a = 5;
A = 6;//error:assignment of read-only variable ' a '
int *p;
p = (int *) &a;//here Alarm high can be eliminated by forcing type conversion
*p = 6;
printf ("A =%d.\n", a);//A = 6, the result proves that the variable of the const type has been changed
return 0;
}
Const keywords and pointers