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 3 of 8.
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.
And how to assume a[0]=*(*(p+i)+j) in printf statement.
Kushal baldev said:
9 years ago
Its very simple just apply your basic logic first of all an pointer to array p will be formed and further carry on tasks on the basics for forloop the *p will be {1, 2, 3} apply for loop for I=0 j=0 and j=1 see the total and calculate the index of p and for I=1 j=0 and j=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!
Thank you. Have a great day!
Gopi said:
9 years ago
Thank you for you answer @Sindhu and @Chand.
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?
Akku said:
5 years ago
Right @Astha,
Then why it shows 2, 2, 2, 2 in the second row?
Then why it shows 2, 2, 2, 2 in the second row?
Afreed said:
6 years ago
Nice explanation, thanks @Dilip.
Maha said:
7 years ago
Thank you for solving.
Kuk said:
7 years ago
Nice, Thanks @Dilip.
Jhanu said:
7 years ago
Thank you @Dilip.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers