# Thread: help in uderstaning 2D array

1. ## help in uderstaning 2D array

1)
Code:
```int main ()
{
int a[2][2]={1,2,3,4};
a[1][0]=*(*(a+1)+1); // how this works??
a[1][1]=*(*(a+1)+0);

printf("%d %d %d %d\n",a[0][0],a[0][1],a[1][0],a[1][1]);
return 0;
}```
and the second one is

Code:
```int main ()
{
int p[10];
printf("%d",&p+1);
}```
why it prints -10

2. The second one is quite simple. Open a phone book. Close your eyes and point to a phone number, on some page.

Why does it show that number? That's why it prints -10 (or whatever).

The first snippet, has to do with a. What is a? An address of the first element in the a array.

Now ask yourself, what is the value of a? Have your program print it out for you. Now have it show you what the value of a+1 is. Now have it show you the value of *(a+1), and *a +1, and all the rest.

Work with it until you're satisfied that you KNOW what these expressions mean (and don't mean).

When it's feasible, don't hesitate to enhance your programming skills by experimenting. Learning by doing is a great way to let it really sink in.

3. ## Pointer Arithmetic

In case of first snippet You just accessing array element by specifying its row and column value.

Code:
```int a[2][2]={1,2,3,4};

//  defined array structure
// a  0 1
// 0  1 2
// 1  3 4

a[1][0]=*(*(a+1)+1); // how this works??
//  In above code, first *(a+1) is accessing the second rows of the array
//  Once you reach to second rows , you are accessing the second element of the array
//  So , it is equal to a[1][1]
a[1][1]=*(*(a+1)+0);
//   If you understand first one, So above statement is equal to a[1][0]```

In case of second snippet :

Actually you have not given specific values for that array.
So ,it is accessing some garbage value.

4. The second one giving a negative number shouldn't even be possible. Perhaps a signed/unsigned conversion error?
Unless the & is just a typo for *, then it would just be uninitialized data as Adak and pavun_cool pointed out.

5. Code:
```int main ()
{
int p[10];
printf("%d",&p+1);
}```
There is nothing undefined with that statement.
its printing the address of p[10] ( address of p + size of the array ) interpreted as int.
Kurt

6. Originally Posted by ZuK
There is nothing undefined with that statement.
its printing the address of p[10] ( address of p + size of the array ) interpreted as int.
That corresponds with _Mike's observation, and I think that is indeed what happened. However, I also think that it does result in undefined behaviour: the argument is supposed to be of the correct type for the corresponding conversion specification, but that is not the case here.

7. Code:
```int main() {

int a[2][2] = { 1, 2, 3, 4 };
printf( "%ld %ld\n", a + 1 , *(a + 1));
return 0;
}```
It gives the output
140734762639080 140734762639080

Is a + 1 and *( a + 1) are same ?? . If not, how ?

8. Originally Posted by lsme
Is a + 1 and *( a + 1) are same ?? . If not, how ?
They have the same value, but they are not the same.