0

Possible Duplicate:
How do I use arrays in C++?

In the following code snippet:

int a[2][3][2]={{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};

what will be the answers for a[i],a[i][j] ,where i<=1 and j<=2 I have no problem in understanding the a[i][j][k] working.Can you please explain how does the above indexing works?

Community
  • 1
  • 1
KP_K
  • 83
  • 5
  • I hope [this](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) helps. – PaperBirdMaster Nov 26 '12 at 10:24
  • @PaperBirdMaster Not really. I still cannot understand how , for example a[1]-a[0]=3 .But the article you linked is great.Thanks – KP_K Nov 26 '12 at 10:33
  • `a[i]` is `int ** ` type `a[i][j]` is `int *` type and `a[i][j][k]` is `int` type so all three are different – Omkant Nov 26 '12 at 10:36
  • Maybe this helps http://stackoverflow.com/questions/13551168/how-can-i-create-this-int-value-and-process-it/13551183#13551183 – Alberto Bonsanto Nov 26 '12 at 10:47

4 Answers4

4

I am not entirely sure what your problem is, but I will try to explain the indexing to you:

What you create in your example is a three dimensional array (or a nested array if you will).

I'm sure you understand simple arrays like the following

            ---x---
int a[3] = {1, 2, 3};

Now when you request a[x] the x will determine which position of the array is chosen.

A two dimensional array is merely an array of arrays

                ---------y--------
                ---x---    ---x---
int b[2][3] = {{1, 2, 3}, {4, 5, 6}};

When you request b[y][x] the y will determine which of the two one dimensional arrays is chosen and then the x tells you which position of that array.

A three dimensional array is only taking this one level higher: an array of arrays of arrays

                    ----------------------z--------------------
                    ---------y---------     ---------y---------
                    ---x---    ---x---      ---x---    ---x----
int c[2][2][3] = {{{1, 2, 3}, {4, 5, 6}}, {{1, 2, 3}, {4, 5, 6}} };

Now a request to c[z][y][x] goes to the z-th 2d-array, then to the y-th 1d-array of that 2d-array and then to the x-th position in this array.

The requests to c[z] or c[z][y] will only result in addresses of arrays and not yield any actual int values.

NiklasMM
  • 2,895
  • 2
  • 22
  • 26
1

a[i] and a[i][j] are addresses, it has nothing to do with the values specified in int a[2][3][2]={{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};. That means a[1]-a[0] is the number of bytes between the second array pointed by a and the first one.

Waterfrag
  • 517
  • 4
  • 20
  • I cannot understand what you meant by "the number of bytes between the second array pointed by a and the first one".Can you explain how a[i]-a[0]=3 and a[1][0]-a[0][0]=3 in the code? – KP_K Nov 26 '12 at 10:50
  • Not the number of bytes, the number of memory locations. There are 12 bytes between 3 ints. – Minion91 Nov 26 '12 at 10:51
1
a[0] = {{1,2},{9,8},{3,7}}
a[1] = {{2,2},{1,4},{5,4}}

a[0][0] = {1,2},                 a[0][1] = {9,8},                a[0][2] = {3,7}
a[0][0][0] = 1,  a[0][0][1] = 2, a[0][1][0] = 9, a[0][1][1] = 8, a[0][2][0] = 3, a[0][2][1] = 7

a[1][0] = {2,2},                 a[1][1] = {1,4},                a[1][2] = {5,4}
a[1][0][0] = 2,  a[1][0][1] = 2, a[1][1][0] = 1, a[1][1][1] = 4, a[1][2][0] = 5, a[1][2][0] = 4
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
1

The thing you have to remember here is that the there are 2 different objects you are working with.

a[i][j][k]

and

a[i][j], a[i] and a

The first is an int, the other 3 are pointers

The associated memory's look like this:

a       : | a[0] | a[1]|
             \/     \______________________
a[]     : | a[0][0] | a[0][1] | a[0][2] | a[1][0] | a[1][1] | a[1][2] |
             \/         \______________
a[][]   : | a[0][0][0] | a[0][0][1] | a[0][1][0] | a[0][1][1] | ...

And so on...

Now in the example you make you do arithmic operations with pointers eg. a[1] - a[0] = 3. If you look at the memory associated wit that you can see there are 3 memory locations between the 2 pointers, thus the result is 3.

Minion91
  • 1,911
  • 12
  • 19
  • Very well explained .I didnt know that the later 3 forms are pointers .Understanding that helped me ,Thanks a lot :) – KP_K Nov 26 '12 at 11:08