C++ Array subscript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ Array subscript

Why does this give 4? What kind of syntax is this? int mat[2][2] = {{1,2},{3,4}}; cout << 1[mat[1]];

30th Jan 2019, 2:46 PM
. �
.                                                � - avatar
1 Answer
+ 3
Probably easier to imagine a 1D array first. arr[1] is the same as *( arr + 1 ) If you switch them around, 1[arr], you get *( 1 + arr ) Since the + operator is commutative the result stays the same. So for your 2d array, 1[mat[1]], it becomes *( 1 + *( mat + 1 ) ) instead of mat[1][1], *( *( mat + 1 ) + 1 ).
30th Jan 2019, 5:35 PM
Dennis
Dennis - avatar