Search code examples
c++arrayspointersstandardssubscript-operator

What type does C++ expect for array subscripts?


In C, array subscription: a[b] is merely the syntactic sugar equivalent of dereferencing after pointer arithmetic: *(a+b) (as explained, say, here).

How is array subscription interpreted in C++, for base types? (Not for classes, for which we have overload semantics)? And, more specifically, what type does C++ expect to appear as the subscript? Is it a ptrdiff_t?


Solution

  • C++ is exactly the same as C in this respect. C++11 §5.2.1:

    The expression E1[E2] is identical (by definition) to *((E1)+(E2))