C Programming · Interview question

What does arr[i] actually compile to?

A strong answer

*(arr + i). The subscript operator is defined in terms of pointer arithmetic: the array name decays to a pointer to its first element, you add i elements to it, and dereference. Because addition commutes, arr[i], *(arr + i), and even i[arr] are all the same expression, the last one being a curiosity that proves indexing is pure pointer math. The practical upshot is that arrays and pointers share the same access mechanism, which is why a function taking int * can be indexed with [] and a pointer into an array can be subscripted.

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

Pointer Arithmetic & Arrays

Why arr[i] is literally *(arr + i), how pointer math scales by type size, and the array-to-pointer decay that loses your size the moment you call a function.

More Pointer Arithmetic & Arrays questions

Browse all 472 interview questions