C Programming · Interview question

How do you read the declaration const int *p versus int *const p?

A strong answer

Read right-to-left from the variable. const int *p, "p is a pointer to a const int": you can change which int p points at, but you can't modify the int through p (*p = 5 is an error). int *const p, "p is a const pointer to an int": you can modify the pointed-to int (*p = 5 is fine) but you can't repoint p to a different address. And const int *const p is both: can't change the target and can't modify through it. This matters for const-correctness in APIs, a function taking const int * promises not to modify your data, which both documents intent and lets the compiler catch accidental writes.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Pointers & Addresses

A pointer is just a variable that holds an address. Master & and *, NULL, and how passing a pointer lets a function reach back and modify the caller's data.

More Pointers & Addresses questions

Browse all 472 interview questions
How do you read the declaration const int *p versus int *const p? | EmbeddedPrep.io