• edited by
4,900 views
6 6 votes

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.

2 Answers

8 8 votes

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

• edited by
0 0 votes

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]

 

Position:
Show:

Related questions

0 0 votes
0 0 answers
552
552 views
Debargha Mitra Roy asked Aug 14, 2024
552 views
If $n$ has the value $3$, then the statement $a[++n]=n++$;(A) assigns $3$ to $a[5]$(B) assigns $4$ to $a[5]$(C) assigns $4$ to $a[4]$(D) assigns a compiler-dependent valu...
0 0 votes
1 1 answer
1.5k
1.5k views
aditi19 asked Sep 13, 2018
1,477 views
what is wrong with this code? It shows segmentation fault#include<stdio.h>#include<stdlib.h>void main(){ int i,j,count=1; int a=(int )malloc(3*sizeof(int*)); fo...
1 1 vote
1 1 answer
670
670 views
Sourabh Kumar asked Jul 25, 2016
670 views
4 4 votes
2 2 answers
8.6k
8.6k views
Anjan asked Jan 8, 2018
8,648 views
Consider 3 dimensional Array A[90] [30] [40] stored in linear array. If the base address starts at 10, The location of A [20] [20] [30] in case of RMO and CMO are _______...