Home > Software engineering >  What happens when we interchange the arrayname and index like index[arrayname] in C ?
What happens when we interchange the arrayname and index like index[arrayname] in C ?

Time:12-04

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?

CodePudding user response:

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.

CodePudding user response:

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

  • Related