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 2 of 8.
Pradeepa said:
8 years ago
Please, anybody explain this concept and tell the answer for it.
#include
void main()
{
int a[2][2]={{2},{3}};
printf("%d",a[0][0]);
printf("%d",a[0][1]);
printf("%d",a[1][0]);
printf("%d",a[1][1]);
}
#include
void main()
{
int a[2][2]={{2},{3}};
printf("%d",a[0][0]);
printf("%d",a[0][1]);
printf("%d",a[1][0]);
printf("%d",a[1][1]);
}
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.
Vinod said:
1 decade ago
Substitute p=1 and i and j as 00,01,10,11 in all the arrays condition in printf.
So that for i=0 and j=0, p=1(every time) we get it as 1111.
Repeat the procedure for all. The answer is correct only.
So that for i=0 and j=0, p=1(every time) we get it as 1111.
Repeat the procedure for all. The answer is correct only.
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.
*(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.
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
Subodh kumar said:
7 years ago
Simple process for solving it;
i < 2
Means i has maximum value i =1
j <2
Same for j
j =1
According to the question;
P + i + j = a[2] = 3.
So, ' C ' is the correct answer.
i < 2
Means i has maximum value i =1
j <2
Same for j
j =1
According to the question;
P + i + j = a[2] = 3.
So, ' C ' is the correct answer.
(2)
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.
Rashmi said:
1 decade ago
Can anyone tell me.
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
What is meant by last line?
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
What is meant by last line?
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.
This gives 2 3 2 3 when i=0, j= 1.
Don't understand this part.
Som said:
8 years ago
Hi,
If the variable 'p' in printf statement is replaced by array name 'a', then we get the output as option 'A'.
Could anyone explain this discrepancy?
If the variable 'p' in printf statement is replaced by array name 'a', then we get the output as option 'A'.
Could anyone explain this discrepancy?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers