edited by
1,083 views
1 votes
1 votes
#include<stdio.h>
int main(){
int (*a)[5];
int arr[5][5]={
          {10,65,300,400,500},
          {100,20,3000,40,5000}
          };
a = arr;
++a ;
char *ptr = (char*)*a;
++a ;
printf("%d %d %d",**a,*arr[1],*ptr);
return 0;
}
edited by

1 Answer

Best answer
3 votes
3 votes

Here arr=$\begin{pmatrix} 10& 65& 300& 400& 500\\ 100& 20& 3000& 40& 5000\\ 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0\\ 0& 0& 0& 0& 0\\ \end{pmatrix}$

arr dimension is 5*5. a is also 2D array in which each row contain 5 elements.

Suppose arr starting address is 1000 and consider integer will take 4 Bytes.

Now, a=arr so pointer a will also point to the address 1000. 

The next step is ++a;

Here, a is 2D array so ++a will skip one row i.e now a points to the address 1020. (1000 + (5*4)).


The next step is char *ptr = (char*)*a;

It will create a character pointer ptr which points to the address 1020.


The next step is ++a;

++a will again skip one row i.e now a points to the address 1040. (1020 + (5*4)).


Now **a means **(a+0) which is element on 1040 address i.e 2rd row 1st element [2][1] which is 0 because here arr is initialize at compile time so all remaining value initialize as 0..

*arr[1] means **(arr+1) which is element on 1020 address i.e 1st row 1st element [1][1] which is 100.

*ptr means value on 1020 adress which is again 100.

So, Output is: 0 100 100.

selected by

Related questions

7 votes
7 votes
3 answers
1
Parshu gate asked Nov 20, 2017
780 views
What is the output of the following program?#include<stdio.h int main() { int array[]={10, 20, 30, 40}; printf(“%d”, -2[array]); return 0; }$-60$$-30$$60$Garbage valu...