Example 1 — Three const pointer forms
Each form allows a different change.
#include <stdio.h>
int main(void) {
int a = 1, b = 2;
const int *p1 = &a; /* data const */
p1 = &b; /* ok: pointer moves */
int *const p2 = &a; /* pointer const */
*p2 = 10; /* ok: data changes */
const int *const p3 = &a; /* both const */
printf("*p1=%d *p2=%d *p3=%d a=%d\n", *p1, *p2, *p3, a);
return 0;
} How It Works
p1may point somewhere else, but*p1 = 5would not compile.p2always points toa, but can changea.p3can neither move nor write; it just readsa, which is now 10.
