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;
}
Discussion:
78 comments Page 7 of 8.
Bhavya said:
1 decade ago
Chand explanation is very easily understood and its good.
Vamsy said:
1 decade ago
From the above program ;
a[0][0]=1 we can write as *(a+0) ; Here 'a' points a[0] and(a+0) to a[0][0]
a[0][1]=2 we can write as *(a+1) ; Here 'a' points a[0] and(a+1) to a[0][0]
a[1][1]=3 " " " " *(*(a+1)+0)
a[1][1]=4 *(*(a+1)+1)
The program goes in this manner.....
a[0][0]=1 we can write as *(a+0) ; Here 'a' points a[0] and(a+0) to a[0][0]
a[0][1]=2 we can write as *(a+1) ; Here 'a' points a[0] and(a+1) to a[0][0]
a[1][1]=3 " " " " *(*(a+1)+0)
a[1][1]=4 *(*(a+1)+1)
The program goes in this manner.....
Neetesh said:
1 decade ago
What about the two dimensional array
a[0][0]=1
a[0][1]=2
a[1][0]=3
a[1][1]=4
i.e. *(*(a+1)+0)=3
*(*(a+0)+1)=2
Is this right??? if yes then please help me to solve it.
a[0][0]=1
a[0][1]=2
a[1][0]=3
a[1][1]=4
i.e. *(*(a+1)+0)=3
*(*(a+0)+1)=2
Is this right??? if yes then please help me to solve it.
Karishma said:
1 decade ago
@Dilip:
There is printing mistake in Step:3 i=1,j=0
Fourth line
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[1]=2
Since i=1 in step:3 therefore,it shoud be
*(*(p+j)+i)=*(*(p+0)+1)=*((a)+1)=a[1]=2
There is printing mistake in Step:3 i=1,j=0
Fourth line
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[1]=2
Since i=1 in step:3 therefore,it shoud be
*(*(p+j)+i)=*(*(p+0)+1)=*((a)+1)=a[1]=2
Naresh said:
1 decade ago
Thank you Dilip.
I could get perfectly from your explanation.
I could get perfectly from your explanation.
Dilip said:
1 decade ago
step1:p=0;
step2:i=0,j=0
*(*(p+i)+j)=*(*(p+0)+0)=*((a)+0)=a[0]=1
*(*(j+p)+i)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(i+p)+j)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[0]=1
step2:i=0,j=1
*(*(p+i)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(j+p)+i)=*(*(1+p)+0)=*((a+1)+0)=a[1]=2
*(*(i+p)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(p+j)+i)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(j+p)+i)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(i+p)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(j+p)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(i+p)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(p+j)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
step2:i=0,j=0
*(*(p+i)+j)=*(*(p+0)+0)=*((a)+0)=a[0]=1
*(*(j+p)+i)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(i+p)+j)=*(*(0+p)+0)=*((a)+0)=a[0]=1
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[0]=1
step2:i=0,j=1
*(*(p+i)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(j+p)+i)=*(*(1+p)+0)=*((a+1)+0)=a[1]=2
*(*(i+p)+j)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(p+j)+i)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
step3:i=1,j=0
*(*(p+i)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(j+p)+i)=*(*(p+0)+1)=*((a)+1)=a[1]=2
*(*(i+p)+j)=*(*(p+1)+0)=*((a+1)+0)=a[1]=2
*(*(p+j)+i)=*(*(p+0)+0)=*((a)+0)=a[1]=2
step4:i=1,j=1
*(*(p+i)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(j+p)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(i+p)+j)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
*(*(p+j)+i)=*(*(p+1)+1)=*((a+1)+1)=a[2]=3
(6)
Kim said:
1 decade ago
*p is an array of pointers each of wich is pointing to an element of the array a.
Lakshmi said:
1 decade ago
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
Can you explain this step.
Can you explain this step.
Peter said:
1 decade ago
Can you explain deeply.
Abin joseph said:
1 decade ago
Thank you Mr.Chand.
I like your explanation very much.
It is very helpfull.
I like your explanation very much.
It is very helpfull.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers