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.

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

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.

Shubham said:   1 decade ago
Can anyone please explain static int a[2][2] = {1, 2, 3, 4}; ?
Also static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};

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)

Trupti said:   1 decade ago
You are absolutely right @Paul.
If
static int *p[] = {a, a+1,a+2};
then o/p is:

1 1 1 1
2 3 2 3
3 2 3 2
4 4 4 4

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)

Anshu said:   1 decade ago
Answer is worng.

Correct answer is.

1 1 1 1.
2 3 2 3.
3 2 3 2.
4 4 4 4.

Execute it on C compiler and check.
(1)

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!

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?

Jai said:   1 decade ago
What means this:- please any one help ?

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


Post your comments here:

Your comments will be displayed after verification.