1 votes 1 votes #include <stdio.h> int main() { int A[20][30]; int i,j,*p; p = &A[0][0]; for(i=0;i<20;i++) { for(j=0;j<20;j++) { *(p+20*j+i) = i*30+j; } } printf("%d\n",A[6][10] - A[6][9]); } O/P ?? Samujjal Das asked Dec 13, 2016 • edited Dec 13, 2016 by dd Samujjal Das 531 views answer comment Share Follow See 1 comment See all 1 1 comment reply Samujjal Das commented Dec 13, 2016 reply Follow Share Answer is given 30. I guess its wrong then? 0 votes 0 votes Please log in or register to add a comment.
Best answer 4 votes 4 votes printf("%d\n",A[6][10] - A[6][9]); $\text{A[6][10]}$ - $7$ th rwo and $11$ th column. How many elements before $\text{A[6][10]}$ in array A ? To count that, above this element, there are $6$ rows ($30*6 = 180$ $\text{elements}$) and in $7$th row we have $10$ elements before $\text{A[6][10]}$. So, total $190$ elments before $\text{A[6][10]}$ in matrix A. $\Rightarrow$ *(p+190) will be the location of $\text{A[6][10]}$. Similarly for $\text{A[6][9]}$, *(p+189) will be the location. Reason behind evaluating locations like this is row major order storage of C style arrays Last thing to do: represent *(p+190) and *(p+189) in terms of the given assignment format i.e *(p+20*j+i) *(p + 190) = *( p + 20*9 +10) $\Rightarrow$ i = 10 and j = 9 $\Rightarrow$ A[6][10] = $10*30 + 9$ = $309$ *(p + 189) = *( p + 20*9 + 9) $\Rightarrow$ i = 9 and j = 9 $\Rightarrow$ A[6][9] = $9*30 + 9$ = $279$ $\Rightarrow$ A[6][10] - A[6][9] $\rightarrow$ 30. dd answered Dec 13, 2016 • edited Dec 13, 2016 by dd dd comment Share Follow See all 5 Comments See all 5 5 Comments reply Show 2 previous comments pC commented Dec 13, 2016 reply Follow Share got it :) 0 votes 0 votes dd commented Dec 13, 2016 reply Follow Share :).... 0 votes 0 votes Prateek kumar commented Dec 17, 2016 reply Follow Share Nice explanation :) 0 votes 0 votes Please log in or register to add a comment.