C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 2)
2.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i), 
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}
1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
1, 2, 1, 2
2, 3, 2, 3
3, 4, 3, 4
4, 2, 4, 2
1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3
1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
78 comments Page 5 of 8.

Anil said:   1 decade ago
How come a[0]=1, a[1]=2 and a[2]=3?

Can any one explain please?

Naresh said:   1 decade ago
Thank you Dilip.

I could get perfectly from your explanation.

Akku said:   5 years ago
Right @Astha,

Then why it shows 2, 2, 2, 2 in the second row?

Saurav said:   1 decade ago
@sindhu:

Why p=0 in step 1?

Please explain it more clearly.

Yogesh said:   1 decade ago
a[j][k] is equivalent to *((base-type *)a+(j*row length)+k).

Aabid khan said:   1 decade ago
I could not get the logic of this question please explain.

Bhavya said:   1 decade ago
Chand explanation is very easily understood and its good.

Nikhil said:   8 years ago
Please explain this question how the compiler works?

Induja said:   1 decade ago
p is the static pointer. So it is initialized as 0.

Shanthi priya.kota said:   10 years ago
How can you get p = 0?

Can you please explain me?


Post your comments here:

Your comments will be displayed after verification.