I read many old questions, many answers about const
keyword in a declaration to deeply understand the syntax of declarations.
I played with const
and I am now a little bit confused with this example :
int i;
i=1;
int const *ptr_i;
ptr_i=&i;
printf("%d\n",*ptr_i);
i=2;
printf("%d\n",*ptr_i);
If int const *
is a pointer to a constant integer, why ptr_i=&i;
is allowed ? i
is not a constant integer.
Moreover, if the value of i
is changed from 1 to 2 (obviously, it is possible because i
is not a constant integer), the second printf
displays 2. I am not surprised because the value of i
has been changed and ptr_i
is a pointer to i
.
I, also checked that he value of ptr_i has not changed ( printf("%p ",ptr_i)
).
Where is my misunderstanding ?