1
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ The answer is, yes, don't use it. As you might discover, a[i] is simply the memory location a + i. $\endgroup$
    – Pål GD
    Sep 20 at 14:09
  • $\begingroup$ You did not explain problem with i[a]. $\endgroup$
    – tbhaxor
    Sep 20 at 14:20
  • $\begingroup$ Maybe this can help stackoverflow.com/a/19782170 $\endgroup$
    – Russel
    Sep 21 at 4:55

2 Answers 2

0
$\begingroup$

Since addition is commutative, we can use any accessor method. But in programming, the more code is human-understandable, the better it is ranked. And that is why guidelines exist.

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.

$\endgroup$
-1
$\begingroup$

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].

https://en.cppreference.com/w/cpp/language/operator_member_access

$\endgroup$
2
  • $\begingroup$ This is outside the scope of cs. $\endgroup$
    – Pål GD
    Sep 22 at 9:00
  • $\begingroup$ @PålGD: right, I missed that. $\endgroup$
    – user16034
    Sep 22 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.