Reputation: 99
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?
Upvotes: 3
Views: 283
Reputation: 57
It has no effect since arrayname[index]
is identical writing as index[arrayname]
cause both are interpreted as:
*(arrayname + index)
Upvotes: 1
Reputation: 76305
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.
Upvotes: 4