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

Anonymous said:   8 years ago
Can you explain how p=0 in the first step?

Riya said:   8 years ago
Not understanding. Please explain it for me.

Gopi said:   9 years ago
Thank you for you answer @Sindhu and @Chand.

Debajyoti Mallick said:   9 years ago
p will not be zero. It is just holding the base address of the second array.

Thank you. Have a great day!

Kushal baldev said:   9 years ago
Its very simple just apply your basic logic first of all an pointer to array p will be formed and further carry on tasks on the basics for forloop the *p will be {1, 2, 3} apply for loop for I=0 j=0 and j=1 see the total and calculate the index of p and for I=1 j=0 and j=1.

Cse said:   9 years ago
Why you take it: static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};

And how to assume a[0]=*(*(p+i)+j) in printf statement.

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

Can you please explain me?

Priya said:   10 years ago
Some more clearly please.
(1)

Balamanikanta said:   10 years ago
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2}; please explain this step.

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


Post your comments here:

Your comments will be displayed after verification.