C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 22)
22.
What will be the output of the program ?
#include<stdio.h>
int main()
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for(i=0; i<=4; i++)
printf("%d, ", a[i]);
return 0;
}
void change(int *b, int n)
{
int i;
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
}
Discussion:
52 comments Page 2 of 6.
Nakul said:
8 years ago
What is the prototype of change function?
Devendar said:
8 years ago
Guys, actually the function is void and it not returning any values.
Then why you are changing the array values?
The answer is same as array: 2 4 6 8 10.
Then why you are changing the array values?
The answer is same as array: 2 4 6 8 10.
Anitha said:
8 years ago
I understood the concept.
But why do we change the second element alone? Please anyone describe it for me.
But why do we change the second element alone? Please anyone describe it for me.
Mahesh said:
8 years ago
According to the logic, answer should be 7, 9, 11, 13, 15. But answer shows 2, 15, 6, 8.
How it is correct?
How it is correct?
Mahesh said:
8 years ago
1. The problem is in the form of call by value. So it has to print in called method. But in the given problem, printf statement was in the calling method.
Rahul W said:
9 years ago
Nice and well explanation, Thank you all guys!
Sujitha S said:
9 years ago
Which is the correct answer A or B?
Robert said:
9 years ago
Since i = 0 and i < 4 => i can take maximum value 4.
Elements in array are : a[0] = 2 ... a[4] = 5;
Now let`s think at the last for loop : where i takes maximum value of 4 (4 < 5) :
*(b+1) = *(b+i)+5; can be translated like :
b = first address from the array, hence 0 (since base address is always 0)
*(0+1) = *(0 + 4) + 5 => the value of the second element of the array a[0+1] = a[0+4] + 5 => second element = a[4] + 5 = 10 + 5 = 15.
Elements in array are : a[0] = 2 ... a[4] = 5;
Now let`s think at the last for loop : where i takes maximum value of 4 (4 < 5) :
*(b+1) = *(b+i)+5; can be translated like :
b = first address from the array, hence 0 (since base address is always 0)
*(0+1) = *(0 + 4) + 5 => the value of the second element of the array a[0+1] = a[0+4] + 5 => second element = a[4] + 5 = 10 + 5 = 15.
(1)
Kajal said:
10 years ago
See everyone here the function is passed by reference. So changes will occur moreover in the statement.
For (i=0; i<n; i++).
* (b+1) = * (b+i) +5;
N=5;
So * (b+1) is the second element. Thus in first iteration value of 2nd element i.e. 4 will become.
For i=0; 2+5=7.
i=1;then (b+1)=4+5=9.
i=2;then (b+2)=6+5=11.
i=3;then (b+3)=8+5=13.
i=4;then (b+4)=10+5=15.
So only change 2 second element hence output.
2 15 6 8 10.
For (i=0; i<n; i++).
* (b+1) = * (b+i) +5;
N=5;
So * (b+1) is the second element. Thus in first iteration value of 2nd element i.e. 4 will become.
For i=0; 2+5=7.
i=1;then (b+1)=4+5=9.
i=2;then (b+2)=6+5=11.
i=3;then (b+3)=8+5=13.
i=4;then (b+4)=10+5=15.
So only change 2 second element hence output.
2 15 6 8 10.
Manikanth said:
1 decade ago
Void type cannot return anything to the main function.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers