I wrote a cast that compiled, ran fine, and was still wrong. Figuring out why finally made me learn how to read C pointer types properly.

The mistake

execv wants its argv as char *const argv[]. I wanted mine fully const:

const char *const argv[] = {"/bin/sh", nullptr};
execv(argv[0], argv);
error: passing argument 2 of 'execv' from incompatible pointer type [-Wincompatible-pointer-types]
note: expected 'char * const*' but argument is of type 'const char * const*'

So I cast it to what I thought execv wants:

execv(argv[0], (char * *const)argv);

It compiled and worked, but Claude flagged it in a review: the cast was wrong, and only worked because of some implicit compiler rules.

Think in boxes

A pointer is a box holding the address of another box. Here’s my argv:

 argv (array, 2 slots)          .rodata (read-only)
┌──────────────┐
│ slot 0: ─────┼─────────────▶ "/bin/sh\0"
├──────────────┤
│ slot 1: null │
└──────────────┘

So there are two levels: the slots (char *) and the chars they point to. const marks one level as read-only. Every pointer type boils down to: how many levels, and which ones are const?

Note there’s no box holding “the address of the array”. An array isn’t a pointer, the name argv is the slots (sizeof argv is 16, a pointer is 8).

The method: move const right, then read right to left

const applies to whatever is immediately to its left, and only if there’s nothing there, to its right. That exception is why const char and char const mean the same thing.

So get rid of the exception: move every leading const after the thing it modifies, then read right to left starting at the name ([] right after the name comes first).

const char *const argv[]
→ char const * const argv[]

                     argv     argv is
                         []   an array of
             * const          const pointers to
char const                    const char

For a cast, put a placeholder name back first:

(char * const *)   →   char * const * X    X is a pointer to a const pointer to char
(char * * const)   →   char * * const X    X is a const pointer to a pointer to char

Same tokens, different order, different box made const.

What const protects on my argv

WriteWhat it changesResult
argv[0][0] = 'x'a char❌ assignment of read-only location '*argv[0]'
argv[0] = "x"a slot❌ assignment of read-only location 'argv[0]'
argv = ...the array itself❌ assignment to expression with array type

The last one doesn’t mention const: arrays can never be assigned. So two consts is all an array needs.

Through a pointer: what execv actually receives

execv doesn’t get my array, it gets a pointer to its first slot. So inside execv there’s a third box:

char const *const *p = argv;   // p holds &argv[0]
   p                argv (slots)          .rodata
┌──────┐        ┌──────────────┐
│  ────┼──────▶ │ slot 0: ─────┼──────▶ "/bin/sh\0"
└──────┘        ├──────────────┤
                │ slot 1: null │
                └──────────────┘

Each of the three can be const or not (checked with GCC 15):

DeclarationRead it asp = ...p[0] = ...p[0][0] = 'x'
char **ppointer to pointer to char✅✅✅
char *const *ppointer to const pointer to char✅❌✅
char **const pconst pointer to pointer to char❌✅✅
char const **ppointer to pointer to const char✅✅❌
char const *const *ppointer to const pointer to const char✅❌❌
char const *const *const pconst pointer to const pointer to const char❌❌❌

Rows 2 and 3 are my mistake. execv wants row 2: “I won’t repoint your slots”. My cast wrote row 3: “this pointer can’t be reassigned”.

Why the wrong cast still worked

Three implicit rules hid it.

1. Top-level const on a cast does nothing. A cast produces a value, not a variable, so there’s nothing to protect. (char **const)argv is just (char **)argv.

2. Arrays decay to pointers. My argv became char const *const * when passed, the type in GCC’s note. And char *const argv[] as a parameter is char *const *argv.

3. C adds const one level in, silently, and nothing more.

char **pp = ...;
char *const *a = pp;          // ✅
char const **b = pp;          // ❌ incompatible pointer type
char const *const *c = pp;    // ❌ (C++ allows it; C doesn't)

So my cast stripped both consts down to char **, and the call quietly added one back. Right type, wrong reasoning.

Line c is also why execv isn’t fully const: callers with a char ** would all need a cast. POSIX instead guarantees exec never modifies the strings (man 3p exec, RATIONALE).

The correct cast

execv(argv[0], (char *const *)argv);

It drops only the const on the chars, which is safe because of that POSIX guarantee. Nothing else changes behind my back.

Check your reading: cdecl

cdecl.org translates C declarations to English and back:

explain char * const * x
→ declare x as pointer to const pointer to char

References: man 3 exec, man 3p exec.