Settings

Theme

C's index[arr] notation

5 points by leegao 15 years ago · 6 comments · 1 min read


I had always assumed that it was merely some strange artifact of the language, until I finally thought about it, and then finally understood:

arr[index] is equivalent to (arr + index), which by the commutative property of addition is equivalent to (index + arr) = index[arr].

Mind = blown

marcomonteiro 15 years ago

It's not the same as (index + arr). arr is a pointer to the beginning address of a series of particular "types". These types have a specific size in memory. Index increments the pointer by (n * size_of(type)).

arr[index] == arr + (n * size_of(type)) == (n * size_of(type)) + *arr

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection