const pointer in C: solved exercise

const pointer in C: solved exercise

If you searched for a solved const pointer in C, here are the three combinations of const with pointers, with examples showing what is allowed and what the compiler rejects.

The rule is to read the declaration right to left: the const closest to the variable name freezes the pointer itself; the const closest to the base type freezes the pointed-to value.

Problem statement

Declare and use the three variants:

  1. const int *p — pointer to constant integer (cannot modify the value).
  2. int * const p — constant pointer to integer (cannot change where it points).
  3. const int * const p — constant pointer to constant integer.

Show which operations are valid and which are invalid for each one.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>

int main(void) {
    int x = 10, y = 20;

    /* ── 1. Pointer to constant integer ─────────────────────────────
       The pointer can change where it points,
       but cannot modify the pointed-to value.                */
    const int *p1 = &x;
    printf("p1  -> %d\n", *p1);

    p1 = &y;                    /* OK: change target */
    printf("p1  -> %d\n", *p1);
    /* *p1 = 99; */             /* ERROR: cannot modify the value */

    /* ── 2. Constant pointer to integer ─────────────────────────────
       Cannot change where it points,
       but can modify the pointed-to value.                   */
    int * const p2 = &x;
    printf("p2  -> %d\n", *p2);

    *p2 = 42;                   /* OK: modify the value */
    printf("p2  -> %d  (x=%d)\n", *p2, x);
    /* p2 = &y; */              /* ERROR: cannot change target */

    /* ── 3. Constant pointer to constant integer ─────────────────────
       Cannot change either the target or the value.          */
    const int * const p3 = &x;
    printf("p3  -> %d\n", *p3);
    /* *p3 = 99; */             /* ERROR: cannot modify the value */
    /* p3 = &y; */              /* ERROR: cannot change target */

    return 0;
}

Expected output

1
2
3
4
5
p1  -> 10
p1  -> 20
p2  -> 10
p2  -> 42  (x=42)
p3  -> 42

Common mistakes

  • Confusing what is constant: const int *p freezes the value, not the pointer; int * const p freezes the pointer, not the value.
  • Assigning a const pointer to a non-const pointer without a cast: int *q = p1; triggers a compiler warning because the const protection is lost.
  • Trying to modify the value through a const int * by casting away const: this is undefined behavior if the original object was declared const.
  • Not initializing an int * const at declaration: a constant pointer must be initialized where it is declared (it cannot be reassigned later).

Practical use

const int * is used in function parameters to guarantee that the original data is not modified (strcmp, strlen, printf all use const char *). int * const appears in embedded hardware registers where the address is fixed but the value is writable. const int * const expresses a fully immutable pointer, useful for read-only configuration tables.

Guided practice and full book

If you want a complete path with progressive difficulty:

FAQ

Why does the compiler warn when assigning const int * to int *?

Because losing the const opens the possibility of modifying data the programmer marked as immutable. The compiler issues a warning (or error in strict mode) to signal that protection is being silently dropped.

What does const mean in the const char *s parameter of printf?

It means that printf commits to not modifying the string pointed to by s. It is a guarantee to the caller: you can safely pass a string literal (which lives in read-only memory) without risk of printf modifying it.

How do you read const int * const *pp?

Right to left: pp is a non-constant pointer (*pp) pointing to a constant pointer (* const) to a constant integer (const int). In practice, double indirection is rare but appears in tables of pointers to read-only strings such as const char * const argv[].