1. ## pointers and arrays

helloo alll,
i am new to this C board. and i am very basic to this language. and i hope u all will help me out to know more about this. any way i have quick question about pointer,how do access the 2d array using the pointer arithmatic. and this is what my approch

Code:
```for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\n",*(a+i*3+j));
}
}```
outut
Code:
```2293584
2293596
2293608
2293620
2293632
2293644```

is this way of accessing the elements
Code:
`*(base address + row(i) * no.ofcol + col(j))`
but when i change this
Code:
`printf("%d\n",*(*a+i*3+j));`
it gives me the proper output expecteed

can any one explain me or if i am wrong correct me please

thax

-onthewaytoC

2. Code:
```int a[2][3] = { { 1, 2, 3}, {3, 6, 9 } };
int i, j;
for( i = 0; i < 2; ++i )
for( j = 0; j < 3; ++j )
printf("%d\n",*(*(a+i)+j));```
The identifier "a" can be looked at as a pointer-to-pointer-to-int (int **a). "a+i" is a pointer-to-pointer-to-int of row "i" of the 2d array which when dereferenced as in "*(a+i)" yields a pointer-to-int indicating the particular row. Adding "j" to this as in "*(a+i)+j" indicates a pointer-to-int to the particular column "j" of the particular row "i" we are looking at. Dereferencing that as in "*(*(a+i)+j)" yields the value at row "i" column "j".

3. Consider this

Code:
``` int arr[4][2]={
{1,2},
{3,4},
{5,6},
{7,8}
};```
Now , the compiler knows how many columns are there in the
array arr ,since we specified this in the array declaration .
So it interprets the expression arr[0] as (arr+0), and arr[1] as
(arr+1).
This way we are able to reach each individual row . Now
suppose I want to refer to the element arr[2][1] using pointers.
arr[2][1] can also be interpreted as (arr[2]+1) . value at this
* i.e *(arr[2]+1) . arr[2][1] can be interpreted as *(*(arr+2)+1)

hence all these expressions are same.

arr[2][1]
*(arr[2]+1)
*(*(arr+2)+1)

so if i have understood you , that the indentifier 'a' is pointer to pointer - an int right

when i increment 'a' thats is
Code:
`which when dereferenced as in "*(a+i)"  yields a pointer-to-int indicating the particular row`
so if i am incementing 'a' the offset is base address(i.e a) + i * 4bytes is it.

if i am wrong please correct me

thax u

-onthewaytoC

5. > The identifier "a" can be looked at as a pointer-to-pointer-to-int (int **a)
Erm, no it isn't, and no it can't.

> int a[2][3] = { { 1, 2, 3}, {3, 6, 9 } };
When you say 'a', you get a pointer to the first element of a (the type being int (*)[3])
This is NOT the same as int**
http://www.eskimo.com/~scs/C-faq/q6.3.html

So when you do (a+i), you're really getting a pointer to a[i] (with the aforementioned type). In memory terms, you now have a pointer to the start of an array containing say 3,6,9 (when i=1).

Now when you do *(a+1), this is when you get an int* pointer.
(*(a+1)+j), you have a pointer to a specific element of 3,6,9 (say a pointer to a memory location containing 9, when j=2 say).

*(*(a+1)+j) dereferences that pointer to an int to get the actual integer (9 say).

Code:
```#include <stdio.h>

int main ( void ) {
int a[2][3] = { { 1, 2, 3}, {3, 6, 9 } };
int i, j;
for( i = 0; i < 2; ++i )
for( j = 0; j < 3; ++j ) {
int (*p1)[3] = a;
int *p2;
int val;
p1 += i;
p2 = *p1 + j;
val = *p2;
printf("%p %p %d\n",(void*)p1,(void*)p2,val);
}
return 0;
}```
Simply mushing p1 to be an int** for example will get you
Code:
```\$ gcc -Wall bar.c
bar.c: In function `main':
bar.c:8: warning: initialization from incompatible pointer type
\$ ./a.exe
Segmentation fault (core dumped)```

6. to access arrays, you need to type array[which_one_it_is] = " whatever you want its value to be ";

I am a very smart programmer, so don't mess with me
DONT

7. >to access arrays, you need to type array[which_one_it_is] = " whatever you want its value to be ";
You don't need to, though that's by far the easiest way to get an an element in the array. However, your post really has no point since the question was concerning how to access arrays using pointer arithmetic, not the subscript operator.

8. I am a very smart programmer
Really.

9. thax very much guys i got it now

-onthewaytoC