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

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.

Shweta said:   1 decade ago
Here, a[2][2] is a 2D array and int*p is a 1D array of pointers.

So, what is getting stored in p[] is the addresses of the first, second and third elements of a[2][2], that are having difference between consecutive locations as 4 bytes only,

i.e p[0] = &a[0][0],
p[1] = &a[0][1],
p[2] = &a[1][0].

Hence, *p[0] = a[0][0] = 1,

*p[1] = a[0][1] = 2,
*p[2] = a[1][0] = 3.

Also, it is to be noted that in an array p[], *(i+p) is same as *(p+i).

So, *(*(p+i)+j) and *(*(i+p)+j) will evaluate to same value and so does *(*(p+j)+i) and *(*(j+p)+i).

And also, *(p+i) is nothing but the value p[i].

Now, *(*(p+0)+0) = *(p[0]+0) = *(&a[0][0]+0) = *(&a[0][0]) = a[0][0] = 1.

*(*(p+0)+1) = *(p[0]+1) = *(&a[0][0]+1) = *(&a[0][1]) = a[0][1] = 2.

*(*(p+1)+0) = *(p[1]+0) = *(&a[0][1]+0) = *(&a[0][1]) = a[0][1] = 2.

*(*(p+1)+1) = *(p[1]+1) = *(&a[0][1]+1) = *(&a[1][0]) = a[1][0] = 3.

As &a[0][1]+1 will point to the next memory location a[1][0], with the difference in starting address of 4 bytes, and so on.
(7)

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

Can any one explain please?

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};

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

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

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

Arun said:   1 decade ago
Why more operators are used in variable?

Does it show different meaning?

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.

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


Post your comments here:

Your comments will be displayed after verification.