woundr
1
I have a 2D array ,int arr[3][3]
arr[0][0] contains a value 10
arr[0][1]=20
arr[0][2]=30
when i try to print (*arr) , i got the base address of arr[0][1].
and also printing (*arr+1) give base address of arr[0][1].
why this happens?is it return the value at that index? then i try to print **arr got the value 10.
what is the logic behind this? please explain?
Thanks.
Show your complete code. Use Code Tags.
gcjr
3
woundr:
I have a 2D array ,int arr[3][3]
arr[0][0] contains a value 10
arr[0][1]=20
arr[0][2]=30
when i try to print (*arr) , i got the base address of arr[0][1].
and also printing (*arr+1) give base address of arr[0][1].
if *arr+1 is arr[0][1], wouldn't you expect *arr to be arr[0][0], not arr[0][1]?
woundr:
then i try to print **arr got the value 10.
isn't this correct?
when you do pointer arithmetic, when you add (subtract) a value from a pointer, it's just like indexing into an array.
of course, if the pointer is to an "int" which is 2 bytes, adding 1 to a pointer adds 2 to the pointer address.
for a 2-dimensional array, the values are of course linearly organized in memory. in your case
arr [0][0]
arr [0][1] *arr+1
arr [0][2]
arr [1][0] *arr+3
arr [1][1]
arr [1][2]
arr [2][0]
arr [2][1] *arr+7
arr [2][2]