Array Supscripting
When selecting an element like this:
#include <stdio.h>
int main() {
int arr[] = {1,3,5};
printf("arr[1]: %d\n", arr[1]);
return 0;
}
arr[1]: 3
When doing arr[i]
the compiler converted to *(arr + i)
. Based on Array arithmetic, we know that arr + i
will select the ith
element and then *(...)
will dereference it and get the value.
But doing so will also mean that we can do *(i + arr)
hence i[arr]
or 1[arr]
#include <stdio.h>
int main() {
int arr[] = {1,3,5};
printf("arr[1]: %d\n1[arr]: %d\n", arr[1], 1[arr]);
return 0;
}
arr[1]: 3
1[arr]: 3
[!note]
Doing*(arr + i)
will have a bit better performance comparing toarr[i]
since the compiler will convertarr[i]
to*(arr + i)
anyways