edited by
534 views
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 ??

edited by

1 Answer

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.

edited by

Related questions

61
views
1 answers
1 votes
kirmada asked 2 days ago
61 views
#include <stdio.h> int main() { // Write C code here int i=10,*p,**q,***r; p=&i; *p=15; q=&p; **q=20; r=&q; ***r=*p+1; printf("%d",i); return 0; }answer the output as integer _________
70
views
1 answers
1 votes
kirmada asked 2 days ago
70 views
#include <stdio.h> int main() { int (*a)[2]; int arr[4][4]={1,2,3,4,6,7,8,9}; a=arr; ++a; printf("%d",**a); return 0; }what is the answer NUMARICAL--------------------------------?
47
views
1 answers
1 votes
shivamSK asked 3 days ago
47 views
#include <stdio.h> void f(int (*x)(int)); int myfoo(int i); int (*foo)(int) = myfoo; int main() { f(foo(10)); } void f( ... i; }sanfoundry C programming Questiona) Compile time errorb) Undefined behaviourc) 10 11d) 10 Segmentation fault 
67
views
2 answers
1 votes