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.

Prabha said:   1 decade ago
Why p=0 ?

int *p[] = {(int*)a, (int*)a+1, (int*)a+2};

What is this? Can anyone explain in clearly?

Monika said:   1 decade ago
ThanQ Delip

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

Shibu said:   1 decade ago
@Subham.

*(p+1)-> the value at p+1 is a+1.

Now *((a+1)+0) -> *(a+1){a refers to base address of array 'a',which is at location 0}.

Hence we can write it as a[1].

The value at a[1]=2.

Subham said:   1 decade ago
Please explain how *(*(p+1) +0) =a[1].

Priya said:   1 decade ago
Here we use static type 'p'.

That's why default value of p is 0.

Riya said:   1 decade ago
Please explain clearly why p=0?

Mr. x said:   1 decade ago
Why p is 0 ?


Post your comments here:

Your comments will be displayed after verification.