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.
| Declaration | Read as | Change the value through p? | Point p somewhere else? |
|---|---|---|---|
const int *p | p is a pointer to an int that is const | No (*p = 5 fails) | Yes (p++ is fine) |
int const *p | same as above; const binds to int | No | Yes |
int *const p = &x; | p is a const pointer to int | Yes | No (must be initialised) |
const int *const p = &x; | a const pointer to a const int | No | No |
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 addingconstis always allowed. Removing it needs a cast, and writing through a pointer whose target was originally const is undefined behaviour.
Interview follow-ups
constdoes 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 toconst 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.
