# Thread: [B]a basic doubt !![/B]

1. ## [B]a basic doubt !![/B]

i just run the following code.

Code:
```#include<stdio.h>
main( )
{
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf("%u %u %u %d \n",a,*a,**a,***a);
printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
getch();
}```
and got the following result

1245008 1245008 1245008 2
1245032 1245016 1245012 3

but i am not getting the concept behind the output,
so anybody plz throw some light on it

thanks!
sam

2. the first 3 numbers of each line are memory addresses. the last number is a value stored in the array.

i won't get into the details, but a[0] points to 1245008 or a[0][0], which points to 1245008 or a[0][0][0] or the value 2. the address of "a" is also 1245008.

if you want to get down in there and study it, add the loop below
Code:
```int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf("%u %u %u %d \n",a,*a,**a,***a);
printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);

for(int i = 0; i < 2; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k < 2; k++)
{
printf("The memory location %u holds the value %d (a[%d][%d][%d]) \n",
&a[i][j][k], a[i][j][k], i,j,k);
}```

but just tell me

***a+1=3?
i mean which 3 and why??

4. disclaimer: technically, some of the following may be inaccurate. i was going for logic, which may also be inaccurate. however, it should definitly be accurate enough to get your mind going in the right direction

basically, a multi-dimensional array is simply just an array.

let's look at at one dimensional array. (let's pretend that an int is one byte, which it isn't)

int a[10];

this reserves 10 bytes of memory. "a" is really a pointer (again, some may argue, but for the sake of simplicity, let's not).
it points to that first byte in memory that is reserved for the array.
all the bytes are guarenteed to be in sequential order when the
array is declared as such. when you ask for a[5], the compiler
finds the address that "a" points to. then, it will move "up" 5
bytes in memory and return the value stored there. a[5] is the
same as *a+5. "a" is referred to as a segment. the five is referred to
as the offset. in short, a[5] and *a+5 says give me the value that is stored in
five bytes past "a".

a two-dimensional pointer acts the same way, only, now
it has to resolve not one, but two offsets. say we declare:

int b[4][10];

the compiler reserves 40 bytes in memory (4 *10), all in sequential order.
b[0][0] is the first and b[4][10] is the last. when you ask for
something like b[2][5], the compiler then "divides" the 40 bytes
it reserved into 4 different sections, or sub-segments. it will then
seek out the 3rd sub-segment (b[2][0], remember, we count 0,1,2,3). after it finds that sub-segment, it
will then move up 5 more memory addresses and return the value located there.

(the following is not tested)
&b[2][5] should be equal to &b((4*10) / 4 * 2 + 5)

multi dimensional arrays work on the same principle, only it will
divide the array further into sub-sub-segments and sub-sub-sub-segments
and so on.

just so you don't get confused, i was pretending that and int was one byte for simplicity.
in reality, it's not. when you say a+5 it REALLY means the address "a"
points to plus (5 * sizeof(int)). but the compiler already does the sizeof(int) calculation
so you don't have to.

just to demonstrate a multidimensional array is simply just an array,
Code:
``` int *b = &a[0][0][0];
for(int l = 0; l < 12; l++, b++)
printf("%d\n", *b);```

***a+1 = 3 would change the first 4 to a 3.

more or less, each * used refers to another level of sub-segments.

***a+1
* says a[0]
** says a[0][0]
*** says a[0][0][0]
***a+1 says a[0][0][1]

EDIT: next 3 lines changed
with **a+1,
the one adds one to the sub-segment, or refers to a[0][1][]
similarily *a+1 refers to a[1][][].

hope this helps

5. ***a+1 = 3 would change the first 4 to a 3.
Not quite. ***a+1 would grab the first index of the array and add 1 to it. ***a+1 isn't a valid lvalue.

Consider the following:
Code:
```itsme@dreams:~/C\$ cat pntr.c
#include <stdio.h>

int main(void)
{
int array[2] = { 3, 86 };

printf("%d %d\n", *array+1, *(array + 1));
return 0;
}
itsme@dreams:~/C\$ ./pntr
4 86```
You would have to do ***(a+1) = 3 to change the first 4 to a 3. Ahh, the joys of precedence.

6. my foux pox

he just threw the ***a+1 = 3 out there and i bit on it...could have sworn i tested it though...oh well

7. thank u guys, u r great!!!1