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 4 of 6.
Thomsika said:
1 decade ago
Can anyone explain me the concept clearly. I could not understand. What is the value of b?
Karthik said:
1 decade ago
for(i=0; i<n; i++)2, 4, 6, 8, 10};
*(b+1) = *(b+i)+5;
In this for loop,only (b+1) location value is updated but not entire array
i.e 1st iteration (b+1)=(b+0)+5<=>2+5=7
2nd iteration (b+1)=(b+1)+5<=>4+5=9
after 5 iterations, the final value that is in (b+1) is
*(b+1)=*(b+4)+5<=>10+5=15;
change() will return an array *b whose *(b+1) location is updated by 15 Hence array a[] will hold values 2,15,6,8,10. since array a[] is passed into array b[](using pass-by-address) hence any changes made in b[] will reflect back in array a[].
*(b+1) = *(b+i)+5;
In this for loop,only (b+1) location value is updated but not entire array
i.e 1st iteration (b+1)=(b+0)+5<=>2+5=7
2nd iteration (b+1)=(b+1)+5<=>4+5=9
after 5 iterations, the final value that is in (b+1) is
*(b+1)=*(b+4)+5<=>10+5=15;
change() will return an array *b whose *(b+1) location is updated by 15 Hence array a[] will hold values 2,15,6,8,10. since array a[] is passed into array b[](using pass-by-address) hence any changes made in b[] will reflect back in array a[].
Loknath said:
1 decade ago
Hello friends
*(b+1)=b[1];
for(i=0;i<n;i++)
b[1]=b[i]+5;
//at last b[1]=2+5......at last b[1]=10+5;
then store in address b[1]=15
ok good day
*(b+1)=b[1];
for(i=0;i<n;i++)
b[1]=b[i]+5;
//at last b[1]=2+5......at last b[1]=10+5;
then store in address b[1]=15
ok good day
Sun said:
1 decade ago
When we Run this program will get the answer B.
How is it ? any one can explain. ?
How is it ? any one can explain. ?
Abi said:
1 decade ago
I can't understand how the answer is option b? someone explains if you knows.
@ashwini said:
1 decade ago
Hey guys i din understand... confused...
If after computin *(b+1)=*(b+i)+5;
we get 7,12,11,13,15
then how cum is OPTION B the answer...
Please help m out!!
If after computin *(b+1)=*(b+i)+5;
we get 7,12,11,13,15
then how cum is OPTION B the answer...
Please help m out!!
Gowthami said:
1 decade ago
Thanku surendra and Atul.
Surendra said:
1 decade ago
@Ramya
a[] = {2, 4, 6, 8, 10}
If you observe the line *(b+1)=*(b+i)+5;
it clearly tells that they want to change the second element.
secondly every time in the loop they are assinging a new value to it.
for i=0; *(b+i) is 2 and +5 equals 7
for i=1; *(b+i) is 7 and +5 equals 12
for i=2; *(b+i) is 6 and +5 equals 11
for i=3; *(b+i) is 8 and +5 equals 13
for i=4; *(b+i) is 10 and +5 equals 15
a[] = {2, 4, 6, 8, 10}
If you observe the line *(b+1)=*(b+i)+5;
it clearly tells that they want to change the second element.
secondly every time in the loop they are assinging a new value to it.
for i=0; *(b+i) is 2 and +5 equals 7
for i=1; *(b+i) is 7 and +5 equals 12
for i=2; *(b+i) is 6 and +5 equals 11
for i=3; *(b+i) is 8 and +5 equals 13
for i=4; *(b+i) is 10 and +5 equals 15
Ramya said:
1 decade ago
Can you explain the answer in detail?
Ashishprabhakar said:
1 decade ago
Here in 2nd last line clearly mentioned
*(b+1) = *(b+i)+5;
that means only *(b+1)will change all other will remain as it is....
*(b+1) = *(b+i)+5;
that means only *(b+1)will change all other will remain as it is....
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers