1,588 views

int arr[2][3][2];

print(arr[1]-arr[0],arr[1][0]-arr[0][0])

P.s :Sorry i can't able to paste the question but this is main Context.

no

it is 3,6

a[1] ==> a means 0th 2D , a+1 means 1st 2D ==> *(a+1) means 0th 1D of 1st 2D

a[0] ==> a means 0th 2D ==> *(a+0) means 0th 1D of 0th 2D

a[1]-a[0] ===> what is the distance between them ?

0th 1D of 0th 2D --> 1st 1D of 0th 2D --> 2nd 1D of 0th 2D --> 0th 1D of 1st 2D

==> 3

similarly, a[1][0] = points to 0th element of 0th 1D of 1st 2D

a[0][0] = points to 0th element of 0th 1D of 0th 2D

a[1][0]-a[0][0] ===> what is the distance between them ?

0th element of 0th 1D of 0th 2D --> 1st element of 0th 1D of 0th 2D --> 0th element of 1st 1D of 0th 2D --> 1st element of 1st 1D of 0th 2D --> 0th element of 2nd 1D of 0th 2D --> 1st element of 2nd 1D of 0th 2D --> 0th element of 0th 1D of 1st 2D.

===> 6

FIRST lof all lets understand what these a, a[0] and a[0][0] means

arr [2] [3] [2] means we have 2 planes in those planes 3 rows and  2 columns

A)   when we simply mention arr  it means arr is a 3d array ===> collection of 2D array's ===> Base address of 0th 2-D address will give as a result.

B) when we mention a[0] means we are derefrencing it  and it means we are pointing to 0th 1-D array of 0th 2-D array

C) when we mention a[0][0] means dereferncing further more one step so now it points to oth element of 0th 1-D array of 0th 2-D array

D) when we mention a[0][0][0] means now we refer to the value of oth element of 0th 1-D array of 0th 2-D array

now see a[1] = *(a+1) ===> *(0th 2-D array + 1) ===> *(1st 2-D array) ===> 0th 1-D of 1st 2-D array

now see a[0] = *(a+0) ===> *(0th 2-D array) ===> 0th 1-D of 0th 1-D array

a[1] - a[0] it is valid if both are point to same array and same size of dimensions, is it valid ? yes, both are pointing in same array and have same size as 1-D then what it means

a[1] - a[0] means how much away a[1] is to a[0] ?

there are 3 steps away.

i.e.,  0th 1-D of 0th 1-D array ---> 1st 1-D of 0th 1-D array ---> 2nd 1-D of 0th 1-D array ---> 0th 1-D of 1st 2-D array

now see a[1][0] = *( *(a+1)+0) ) ===> *( *(0th 2-D array + 1) ) ===> *( *(1st 2-D array) ) ===> *( 0th 1-D of 1st 2-D array ) ===> 0th element of 0th 1-D of 1st 2-D array

now see a[0][0] = *( *(a+0) ) ===> *( *(0th 2-D array) )  ===> *( 0th 1-D of 0th 1-D array ) ===> 0th element of 0th 1-D of 0th 2-D array

a[1][0] - a[0][0] it is valid if both are point to same array and same size of dimensions, is it valid ? yes, both are pointing in same array and have same size as element then what it means

a[1][0] - a[0][0] means how much away a[1][0] is to a[0][0] ?

there are 6 steps away.

i.e., 0th element of 0th 1-D of 0th 1-D array ---> 1st element of 0th 1-D of 0th 1-D array ---> 0th element of 1st 1-D of 0th 1-D array ---> 1st element of 1st 1-D of 0th 1-D array ---> 0th element of 2nd 1-D of 0th 1-D array ---> 1st element of 2nd 1-D of 0th 1-D array ---> 0th element of 0th 1-D of 1st 2-D array

A)   when we simply mention a  it means base address of 3d array .

simply mention a means, 0th 2-D base address

&a means it is base address of 3-D array

Shaik Masthan  i think a means 3d array base address

simply mention a means, 0th 2-D base address

&a means it is base address of 3-D array

but i also see these statements many times thats  why i am confused in past little.

i think we both trying same concept but you take A as base address of 2d array and from there address &A  but i take simply A as 3d and then by DEREFERENCE i go to 2d array

There is a difference.. If you are reading a , then it is 3-d array, but if you are referring it for the address, then it is base address of 2-D
edited

Shaik Masthan bro i am really not getting any idea what you are saying so just take my answer copy it and edit in the comment whereever you think i make mistake :)

i know this may be little big or taking more time but take your time to comment

Shaik Masthan thanks :)

a[1]

a[1][2]

a[1][2][0]

Which of these can't be used as LHS in any assignment statement..

if it is 3-D array, then

a[1]

a[1][2]

can't be used as l-value... but you can use  a[1][2][0]  as l-value.

a[1][2]

is address of 1st integer element in 3nd 1D array of 2st 2D array.

It is address of element .not base address of any array still we can't use it is lvalue ??

means in array only rvalue i.e non address value can be used on left hand side right??

It is address of element, not base address of any array still we can't use it is lvalue ??

yes--- we can't use as l-value due to even though those are memory addresses but not pointers.

a[1][2] = some value ..can be seen as

int x ;

&x = some value

which is invalid right ??
yes !
edited

i.e.,  0th 1-D of 0th 1-D array ---> 1st 1-D of 0th 1-D array ---> 2nd 1-D of 0th 1-D array ---> 0th 1-D of 1st 2-D array

@Deepanshu, @Shaik Masthan Is this line correct?? Considering the given size of 1d array is 2 i.e 0th 1d of 0th 1d array and 1st 1d of 0th 1-d array are indices (0 and 1 indices for array size of 2)possible if so then only 2 steps are required for it right????

Am sorry i was wrong .....got the concept and answer....Tnq a lot for both of you :D :D especially @Shaik Masthan foryour pictorial explanations..

Above 3D array, we can write like this

array[0] => [0] => [0, 1]
[1] => [0, 1]
[2] => [0, 1]
array[1] => [0] => [0, 1]
[1] => [0, 1]
[2] => [0, 1]
array[2] => [0] => [0, 1]
[1] => [0, 1]
[2] => [0, 1]

Clearly Here A[1] and A[0] are 3 steps away.

A[1][0] and A[0][0] are 6 Steps away.

For 4 D array you can write like this.

int array[2][3][2][5];
array[0] => [0] => [0] => [0, 1, 2, 3, 4]
[1] => [0, 1, 2, 3, 4]
[1] => [0] => [0, 1, 2, 3, 4]
[1] => [0, 1, 2, 3, 4]
[2] => [0] => [0, 1, 2, 3, 4]
[1] => [0, 1, 2, 3, 4]
array[1] => [0] => [0] => [0, 1, 2, 3, 4]
[1] => [0, 1, 2, 3, 4]
[1] => [0] => [0, 1, 2, 3, 4]
[1] => [0, 1, 2, 3, 4]
[2] => [0] => [0, 1, 2, 3, 4]
[1] => [0, 1, 2, 3, 4]