# Why is 3[arr] notation of accessing elements in array is not used?

Today, I came across the notation 3[arr] in C++, and I was surprised to find out that it's a valid way to access array elements. For example:

int arr[] = {10, 20, 30};
int element = 1[arr]; // This compiles and works!


Online IDE: https://onlinegdb.com/4o8qnsx8O

The assembly of this looks exactly same

arr:
.long   10
.long   20
.long   30
main:
push    ebp
mov     ebp, esp
sub     esp, 16
mov     eax, DWORD PTR arr+4    ; getting 20 from arr variable, store in eax
mov     DWORD PTR [ebp-4], eax  ; store eax to variable "a"
mov     eax, DWORD PTR arr+4    ; getting 20 from arr variable, store in eax
mov     DWORD PTR [ebp-8], eax  ; store eax to variable "b"
mov     eax, 0
leave
ret


Disassembly from Godbolt: https://godbolt.org/z/dEaYjashs

While this notation seems to work, it's somewhat unconventional, and I'm curious if it's considered a good practice in C++ programming. In natural language, we say "Accessing 3rd element of array arr", then isn't 3[arr] notation fits better here?

My questions are:

1. Is using 3[arr] a valid and safe practice in C++?
2. Are there any specific situations or use cases where this notation might be advantageous?
3. Is there any guideline or convention against using 3[arr] that I should be aware of?

I haven't encountered this notation in any code I've seen so far, so any insights or advice on its usage would be greatly appreciated.

• The answer is, yes, don't use it. As you might discover, a[i] is simply the memory location a + i. Commented Sep 20, 2023 at 14:09
• You did not explain problem with i[a]. Commented Sep 20, 2023 at 14:20
• Maybe this can help stackoverflow.com/a/19782170 Commented Sep 21, 2023 at 4:55

In this case, using arr[3] is more readable than 3[arr]. Instead of array, let's think in term of hashmaps: Suppose you are asked to get third key key in the my_map (and you can use same notion of in hashmap key access), which is more readable "third key"[my_map] or my_map["third key"]. To me, its second one.
The C++ standard says "The built-in subscript expression E1[E2] is exactly identical to the expression *(E1 + E2)...". So 3[arr] is *(3+arr) is *(arr+3) is arr[3].