I need the following quote especially the bold text , to be justified.
... This would tend to imply that somehow
source[i]
is the same as*(p+i)
.In fact, this is true, i.e wherever one writes a[i] it can be replaced with
*(a + i)
without any problems.In fact, the compiler will create the same code in either case. Thus we see that pointer arithmetic is the same thing as array indexing. Either syntax produces the same result. This is NOT saying that pointers and arrays are the same thing, they are not. We are only saying that to identify a given element of an array we have the choice of two syntaxes, one using array indexing and the other using pointer arithmetic, which yield identical results.
This is quoted from the pdf
A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1.2 (PDF Version) Sept. 2003 P.No : 19
a[i]
and *(a + i)
are the same by definition. The C and C++ standards define the syntax a[i]
to be always equivalent to *(a + i)
, to the point where a[i]
and i[a]
are interchangeable.
That does not tell you anything about the relationship between pointers and arrays, because, technically, in both a[i]
and *(a + i)
, a
is an expression of pointer type. The []
operator takes a pointer expression on the left, not an array expression; and the +
operator only operates on a pointer and an integer -- not an array and an integer.
That you can use an array in place of a
is simply a result of the fact that an expression of array type may be implicitly converted to an rvalue expression of pointer type.