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

Sadashiv said:   1 decade ago
One please help me to understood the output of this program.

#include<stdio.h>

void main()
{
long myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
printf("%ld\t",myarr[1][2]);
printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);

}

Manoj said:   1 decade ago
See the difference : No change.
static int *p[] = {(int*)a};//, (int*)a+1, (int*)a+2};

Anil said:   1 decade ago
How come a[0]=1, a[1]=2 and a[2]=3?

Can any one explain please?

Manoj said:   1 decade ago
Try this code : Same output.

*(*p+i+j), *(*p+i+j), *(*p+i+j), *(*p+i+j).

Priyadharshni said:   1 decade ago
printf("%d, %d, %d, %d\n", *(*(p+0)+1), *(*(1+p)+0), *(*(0+p)+1), *(*(p+1)+0));

This gives 2 3 2 3 when i=0, j= 1.

Don't understand this part.

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

SAAGAR said:   1 decade ago
Simply go through this concept you will not get confused and you can save your time in written exams. We know that one * and one + means (*(i+p)) we will get the value of i. In this way we will get ** and ++ also we will get actual value.

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

Sri said:   1 decade ago
Hi. Please explain why we assign p=0?

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

Can you please explain me?


Post your comments here:

Your comments will be displayed after verification.