Back to TILs

C++ trick 01

Date: 2019-03-12Last modified: 2023-09-17

Table of contents

#include <iostream>
int main() {
  int a[3] = {10, 11, 12};
  std::cout << 2 [a] << '\n';
  return 0;

In general 2[a] is identical to a[2] and this is guaranteed to be equivalent in both C and C++ (assuming no operator overloading), because as you meantioned it translates into *(2+a) or *(a+2), respectively. Because the plus operator is commutative, the two forms are equivalent.

Possible output