in Programming in C edited by
1,074 views
1 vote
1 vote
#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;
}
in Programming in C edited by
1.1k views

1 Answer

3 votes
3 votes
Best answer

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

4 Comments

Thanks for the answer but compiler gives the output as 0 , 100, 100
0
0
Oh sorry ..U r right answer is 0 100 100 bcz it is compiled time initialization so compiler initializes all remaining value as 0.
0
0
why compiler initialize as 0, not garbage
0
0
If you just declare arr[5][5] and after that using for loop for initializing array then the remaining value will be garbage. That is known as run time initialization.

But here at declaration time, you initialize an array so compiler designer put initialization code for initialization of array so it depends on compiler designer what should do at compile time initialization.

So It's C language compiler rule.
0
0