Adwaith Rajeev
Adwaith Rajeev

Reputation: 99

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

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

Answers (2)

nonamelogger
nonamelogger

Reputation: 57

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

Upvotes: 1

Pete Becker
Pete Becker

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

Related Questions