i learned about 1D Array , 2D Array , 3D Array.But today , suddenly my friend asked my a question on "how to access Array Elements".
i said "just use array with index".like a[0],a[1] and so on.

and then he replied(it seemed that he knew very well about array) "is it possible 0[a],1[a] and so on ?". i was confused there and tried on the compiler and run it , it run successfully , no error were there.But he could make me satisfied with the logic why the result of a[i] and i[a] are same.

i want to know the logic behind this.
why both are same ?

why both are same ?

The array index operator is syntactic sugar for an offset from a pointer. The pointer is the base address of the array, so a[0] is equivalent to *(a + 0). Because addition is commutative, you can also say *(0 + a), which falls through to the index operator and becomes 0[a].

Note, however, that while this is possible, it's extremely poor practice. Knowing about this bit of trivia is fine for impressing people at parties, but shouldn't be used in any real code.

Edited 3 Years Ago by deceptikon

As deceptikon has explained earlier,
I also wish to add something further to it.
*(0+a) is equal to calling *a or a[0].
when you add the ofset data to the pointer*(2+a) , you just ask the pointer to move to the said memory space then defference the pointer with (*) whiles the pointer has moved to the ofset.

This is called pointer arithmatic.

Edited 3 Years Ago by richieking

thanks for your answers . From your replies i learn another way to access the Array Element(*(a+i) or *(i+a)). Its nice.

however i've not studied on pointer yet but i think that pointers and array are related to each other in some way.

Exactly the same way. A 2D array "decays" to a pointer to an array, so the pointer notation is just an extension of how you do it with a 1D array:

// a[i][j]
*(*(a + i) + j)

Or equivalently:

*(a[i] + j)

Or in the opposite direction:

(*(a + i))[j]

The equivalence means these notations are interchangeable and mixable if you get your parens right.

Edited 3 Years Ago by deceptikon

but it through an error when i use (a+i) with cin>>
i've declared and initialized as below:

a[5]={1,2,3,4,5},i;

and for the purpose to overwrite these values during runtime i wrote statement as below:

cin>>(a+i);

here error is throne. i don't know why ?

Edited 3 Years Ago by Learner010

i is uninitialized in your snippets. Further, you still need to dereference in this case because operator>> is expecting a reference to the object rather than a pointer to it:

#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int main()
{
    int a[] = {1, 2, 3, 4, 5};
    int i = 2;

    cin >> *(a + i);

    copy(a, a + 5, ostream_iterator<int>(cout, " "));
    cout << '\n';
}

You can also use the cpp11 lambda and the for_each in the algorithm as follows.

#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
    int a[] = {1, 2, 3, 4, 5};
    int i = 2;
    cin >> *(a + i);
    copy(a, a + 5, ostream_iterator<int>(cout, " "));

    // Or you can use lambda as follows
    for_each(a,a+5,[](int &c){cout<<c;});// also very useful.

    cout << '\n';
}

Edited 3 Years Ago by richieking

This article has been dead for over six months. Start a new discussion instead.