Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is int[pointer-to-array] in the C++ - standard? [duplicate]

As I have learned, one can write the following code:

char *a = new char[50];
for (int i = 0; i < 50; ++i) {
    i[a] = '5';
}

It compiles. It works. It does exactly the same as

char *a = new char[50];
for (int i = 0; i < 50; ++i) {
    a[i] = '5';
}

Is it just because:

  • a[b] is implemented as a macro *(a + b) by default and the fact that both code samples are valid is just an accident/compiler specific
  • it's standardized somewhere and the outcome of such algorithms should be the same on every platform

It is reasonable to assume that addition should be commutative, but if we implement operator[] in that way, we have made something else commutative, what might not be what we wanted.

The interesting fact is that there is no pointer[pointer] operator, so operator[] is not a macro.

I know it's bad. I know it's confusing the people who read the code. But I want to know if it's just an accident and it will not work in a distant land where unicorns have seven legs and horns are on their left cheek.

like image 426
styrofoam fly Avatar asked May 26 '14 14:05

styrofoam fly


People also ask

Is pointer same as array in C?

In simple words, array names are converted to pointers. That's the reason why you can use pointers to access elements of arrays. However, you should remember that pointers and arrays are not the same. There are a few cases where array names don't decay to pointers.

Is pointer to an array same as pointer to an integer in C?

Pointers and arrays are different, but they are accessed similarly if the pointer is being used to access a block of values. However, the array declares a block of some datatype while a pointer only declares space for itself (the data area needs malloc'd)

Are pointers and arrays same?

An array is considered to be the same thing as a pointer to the first item in the array. That rule has several consequences. An array of integers has type int*. C++ separates the issue of allocating an array from the issue of using an array.

What is the relationship between array and pointer?

An array is represented by a variable that is associated with the address of its first storage location. A pointer is also the address of a storage location with a defined type, so D permits the use of the array [ ] index notation with both pointer variables and array variables.


2 Answers

C++ standard, § 8.3.4, note 7 (page 185) (emphasis mine).

Except where it has been declared for a class (13.5.5), the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2)). Because of the conversion rules that apply to +, if E1 is an array and E2 an integer, then E1[E2] refers to the E2-th member of E1. Therefore, despite its asymmetric appearance, subscripting is a commutative operation.

like image 166
user703016 Avatar answered Oct 31 '22 01:10

user703016


Here is what C++11 standard has to say:

Note: Except where it has been declared for a class (13.5.5), the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2)). Because of the conversion rules that apply to +, if E1 is an array and E2 an integer, then E1[E2] refers to the E2-th member of E1. Therefore, despite its asymmetric appearance, subscripting is a commutative operation. (emphasis is added).

So your assumption that a[b] is implemented as *(a + b) is correct, except that it is implemented directly in the compiler, not as a macro.

like image 44
Sergey Kalinichenko Avatar answered Oct 31 '22 01:10

Sergey Kalinichenko