In embedded C work on NXP Semiconductors platforms such as the MCUXpresso SDK, pointer qualifiers affect API safety, register access, and compiler-enforced correctness. Interviewers use this topic to check whether you can read declarations precisely and reason about mutability.
Explain the difference between a pointer to a constant and a constant pointer in C.
Address these points:
const int *p, int *const p, and const int *const p?Give a clear C-focused explanation, not a generic language-level answer. The interviewer expects you to discuss declaration reading order, valid and invalid operations, and practical usage in firmware or driver code. Small code examples are enough; no long implementation is needed.
In const int *p or int const *p, the data being pointed to is treated as read-only through p. You may change p to point somewhere else, but you may not write to *p through that pointer.
const int x = 10;
const int y = 20;
const int *p = &x;
p = &y; // valid
//*p = 30; // invalid
In int *const p, the pointer itself cannot be reassigned after initialization. However, the value at the pointed-to location may be modified if the underlying object is not const.
int x = 10;
int y = 20;
int *const p = &x;
*p = 30; // valid
//p = &y; // invalid
In const int *const p, both restrictions apply. You cannot reassign the pointer and you cannot modify the pointed-to value through that pointer.
const int x = 10;
const int *const p = &x;
//*p = 20; // invalid
//p = &x; // invalid
A practical way to parse C declarations is to start at the variable name and move outward. This helps distinguish whether const applies to the pointee, the pointer, or both.
const int *p; // p is pointer to const int
int *const p = &x; // p is const pointer to int
Const qualifiers communicate intent and let the compiler catch incorrect writes. In firmware APIs, they help separate read-only buffers, immutable configuration, and writable memory-mapped data.
void NXP_ReadConfig(const uint8_t *src);
void NXP_UpdateValue(uint32_t *dst);