Skip to content
UniCircle
MediumConceptualPointers and const

const int *p vs int *const p vs const int *const p

Question C-PTR-007

Explain the difference between const int *p, int const *p, int *const p and const int *const p. Why do library functions such as strlen take a const char *?

Show the explanation

Read the declaration from right to left, starting at the variable name.

DeclarationRead asChange the value through p?Point p somewhere else?
const int *pp is a pointer to an int that is constNo (*p = 5 fails)Yes (p++ is fine)
int const *psame as above; const binds to intNoYes
int *const p = &x;p is a const pointer to intYesNo (must be initialised)
const int *const p = &x;a const pointer to a const intNoNo

The rule: const applies to whatever is immediately to its left. If nothing is on its left, it applies to what is on its right.

**Why size_t strlen(const char *s)?**

  • It is a promise to the caller: strlen will not modify your string. The compiler enforces the promise inside strlen.
  • It lets you pass string literals and other read-only data without a warning.
  • Callers can pass a plain char *, because adding const is always allowed. Removing it needs a cast, and writing through a pointer whose target was originally const is undefined behaviour.

Interview follow-ups

  • const does not make memory physically read-only. It is a compile-time contract about what this name may do.
  • For pointers to pointers: char ** does not convert implicitly to const char ** in C. Allowing it would open a loophole for modifying const data.
  • Good habit: mark every pointer parameter that a function only reads as const T *. This is called const-correctness.

Original UniCircle question. Last reviewed 22 Oct 2026.