What happens when we interchange the arrayname and index like index[arrayname] in C++?

Viewed 86

What happens when we interchange the array name and index like index[arrayname] in C++? Is arrayname[index] the same as writing index[arrayname]? What will be the value in both?

2 Answers

For builtin types, the definition of E1[E2] "is identical (by definition) to" *((E1) + (E2)). (quotation from [expr.sub]/1) So the answer is simple: interchanging the names has no effect.

For user-defined types, E1 has to be a class type with an overloaded operator[], so, absent some funky stuff, you can't interchange the two expressions.

It has no effect since arrayname[index] is identical writing as index[arrayname] cause both are interpreted as: *(arrayname + index)

Related