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

Chand said:   1 decade ago
p[3]={a,a+1,a+2}
first (*(p+0)+0)=a[0]=1 etc
second *(*(p+0)+1)=a[1]=2 etc
third *(*(p+1)+0)=a[1]=2 etc
and last *(*(p+1)+1)=a[2]=3 etc

Vivek gupta said:   1 decade ago
I can not understand it please explain more.

Chakri said:   1 decade ago
Please explain clearly.

Saurav said:   1 decade ago
@sindhu:

Why p=0 in step 1?

Please explain it more clearly.

Rahul aarthi said:   1 decade ago
i did't get u pls explain clearly

Sindhu said:   1 decade ago
step1:p=0;
step2:i=0,j=0
*(*(p+i)+j)=a[0]=1
*(*(j+p)+i)=a[0]=1
*(*(i+p)+j)=a[0]=1
*(*(p+j)+i)=a[0]=1

step2:i=0,j=1
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=a[1]=2
*(*(j+p)+i)=a[1]=2
*(*(i+p)+j)=a[1]=2
*(*(p+j)+i)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=a[2]=3
*(*(j+p)+i)=a[2]=3
*(*(i+p)+j)=a[2]=3
*(*(p+j)+i)=a[2]=3

Sitakanta Mishra said:   1 decade ago
Anyone please explain this question.

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


Post your comments here:

Your comments will be displayed after verification.