0
int a[10][5];
for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        cout << i[j[a]];
        cout << j[i[a]];
    }
}  

Edit:assume the values are already initialized to the array and is this cout valid then?

please explain the i[j[a]]; part only regardless of the program I'm concerned about that statement only!

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
NEo
  • 3
  • 2
  • What happens when you try it? – Ken White Sep 09 '12 at 04:38
  • @KenWhite I actually ran it and I'm surprised it even compiled http://liveworkspace.org/code/a19889e7dd3e2f1819448f12aa9b6cfe – Rapptz Sep 09 '12 at 04:41
  • the same question is mine if it is compiling then can somebody explain how it is that this statement is valid!!!!!! – NEo Sep 09 '12 at 04:43
  • Your question was "Will this work?". I repeat: What happens when you try it? Your question didn't ask "Will this compile?", which you know it will. Running it will tell you if it works or not. If you're asking something else entirely, then please edit your question so that it contains that information. :-) – Ken White Sep 09 '12 at 04:49
  • 2
    @KenWhite: With the potential for undefined behavior lying around every corner, "trying it out" is not nearly as useful in C++ as it may be in some other languages. – Benjamin Lindley Sep 09 '12 at 05:19

3 Answers3

1
cout << i[j[a]];
cout << j[i[a]];

In C++, the X[Y] notation is lexically equivalent to *(X+Y):

[C++11: 5.2.1/1]: [..] The expression E1[E2] is identical (by definition) to *((E1)+(E2)) [..]

That means that Y[X] is equivalent to *(Y+X), which is the same as *(X+Y) since addition is commutative.

For some reason, the author has decided to try to be "clever" by writing this confusing code.

By commutativity and the definition for X[Y] alone:

i[j[a]] => i[*(j+a)] => *(i+*(j+a)) => *(*(a+j)+i) => *(a[j] + i) => a[j][i]
j[i[a]] => j[*(i+a)] => *(j+*(i+a)) => *(*(a+i)+j) => *(a[i] + j) => a[i][j]

Your cout statements are, then, equivalent to:

cout << a[j][i];
cout << a[i][j];

In any case, the loop attempts to read past the array bounds because there are only 5 integers in each element of the array a, whereas your inner loop attempts to go all the way up to 10.

The practical result of this is undefined, so you could get silent success, silent failure, arbitrary values, a segmentation fault, a power cut, a black hole, a pony for Christmas, a toe amputation or a new bike.

Even when the loop condition is fixed, note that the first statement is semantically incorrect (assuming you continue to ascribe the first dimension to the outer loop and the second dimension to the inner loop).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Love this: "... a power cut, a black hole, a pony for Christmas, a toe amputation or a new bike." – caps Dec 18 '14 at 01:50
0

C arrays have a strange quirk that allows them to be accessed through the "opposite" direction. This is deeply rooted in the pointer arithmetic of arrays. For example, a[1] is equivalent to *(a + 1). Likewise, 1[a] is equivalent to *(1 + a). Due to the commutative nature of addition, this works out quite nicely. More details can be found here.

With that knowledge in tact, the expression i[j[a]] can be broken down into two different parts. j[a] is equivalent to *(j + a) which would return another array due to the multi-dimensional nature of the array you have, for example purposes we'll call this returned array p. Then you have the statement i[p] which would be equivalent to *(i + p). Bringing it back all together would show you that i[j[a]] is equivalent to *(i + *(j + a)). Indeed, this means that i[j[a]] is just an obfuscated way of writing a[j][i].

Community
  • 1
  • 1
Rapptz
  • 20,807
  • 5
  • 72
  • 86
  • Technically not empty, but garbage, and accessing that garbage is UB. – chris Sep 09 '12 at 04:33
  • well that i knew but i attended an job interview and the same question was asked to me and i replied the same then he asked me does this statement makes any sense , how to cout i already know it!!!! – NEo Sep 09 '12 at 04:35
  • can u plese explain how j[i] is same as i[j] – NEo Sep 09 '12 at 04:50
  • @NitishAggarwal look [here](http://stackoverflow.com/questions/381542/in-c-arrays-why-is-this-true-a5-5a) – Rapptz Sep 09 '12 at 04:51
  • @LightnessRacesinOrbit Thanks for reminding me of an answer from September. I guess. – Rapptz Jan 17 '13 at 19:55
  • @LightnessRacesinOrbit I generally get wiser as time goes by, I don't know about others though. I would delete this answer but I don't think I can. Actually, I could probably edit it to make it correct. – Rapptz Jan 17 '13 at 19:57
0

In C/C++, you can either subscript a pointer with an integral index, or subscript an integral index with a pointer, and the meaning (semantics) are exactly the same. I don't know offhand why the heck the insane syntax is valid, but so it is, and apparently so it will remain for ever after.

#include <stdio.h>

int main(int argc, char** argv)
{
  int i = 1, array[10];
  printf("%ld\n", &(i[array])-&(array[i]));
  return 0;
}

Output:

0
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313