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 ?

Recommended Answers

All 9 Replies

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.

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.

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.

You would be right. Arrays decay into pointers when you use them in a name context.

now , my question is that how to access Elements of 2D Array using pointer ?

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.

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 ?

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';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.