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 1 of 8.

Amit said:   4 years ago
@Manju.

You explained it clearly. Thanks.
(1)

Akku said:   5 years ago
Right @Astha,

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

Astha said:   5 years ago
*(*a+1)+0 means a[1][0] element which is 3. So the output should be;

1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
(3)

Mohan said:   6 years ago
@Dilip.

How a+1 points to a[1] ?

a is 2D array
Logically a+1 means 1th 1D array of a.
Please explain briefly.
(1)

Afreed said:   6 years ago
Nice explanation, thanks @Dilip.

Subodh kumar said:   7 years ago
Simple process for solving it;

i < 2
Means i has maximum value i =1
j <2
Same for j
j =1

According to the question;
P + i + j = a[2] = 3.

So, ' C ' is the correct answer.
(2)

Maha said:   7 years ago
Thank you for solving.

Kuk said:   7 years ago
Nice, Thanks @Dilip.

Jhanu said:   7 years ago
Thank you @Dilip.

Venkat said:   7 years ago
Thanks @Pradeepa.
(1)


Post your comments here:

Your comments will be displayed after verification.