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 5 of 6.
Ashishprabhakar said:
1 decade ago
Thanks dev except you all gave wrong information.
Agnivesh said:
1 decade ago
*(b+i) tell the location of array
each time it increment the position
and then update the value
so for i=0,*(b+i) become *b i.e the first location
hence *(b)+5 increase the value by 5 so first loc element is 7
similarly for next location
each time it increment the position
and then update the value
so for i=0,*(b+i) become *b i.e the first location
hence *(b)+5 increase the value by 5 so first loc element is 7
similarly for next location
Aloke said:
1 decade ago
Thanks atul.
Shreya said:
1 decade ago
But won't the pointer will be moving along with the increment?
i.e., during first for loop execution the pointer will move ahead to 4.
So during second for loop, the pointer will be at 4 then how come *(b+1) is 4 for every loop ?
i.e., during first for loop execution the pointer will move ahead to 4.
So during second for loop, the pointer will be at 4 then how come *(b+1) is 4 for every loop ?
Arun tomar said:
1 decade ago
Nice explanation atul.
Keerthi kumar said:
1 decade ago
Thanks Atul.
Gowri said:
1 decade ago
I can t understand this prob how it work.
Some one explain for me.
Some one explain for me.
Dev said:
1 decade ago
/*
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
*/
This code only modifies the value @ (b+1) i.e. if address of b is 1002 then it will increment to 1006 and value @ 1006 is the second element (4) which is modified to *(b+i)+5 i.e. *(b+1)=*(1002+4)+5 => 10+5 => 15 @ the last iteration when the value of i becomes 4.
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
*/
This code only modifies the value @ (b+1) i.e. if address of b is 1002 then it will increment to 1006 and value @ 1006 is the second element (4) which is modified to *(b+i)+5 i.e. *(b+1)=*(1002+4)+5 => 10+5 => 15 @ the last iteration when the value of i becomes 4.
Atul said:
1 decade ago
*(b+1) = *(b+i)+5;
1. i = 0 => *(b+1) ie 4 is replaced by *(b+0)+5 ie 2+5
2. i = 1 => *(b+1) ie 4 is replaced by *(b+1)+5 ie 4+5
3. i = 2 => *(b+1) ie 4 is replaced by *(b+2)+5 ie 6+5
4. i = 3 => *(b+1) ie 4 is replaced by *(b+3)+5 ie 8+5
5. i = 4 => *(b+1) ie 4 is replaced by *(b+4)+5 ie 10+5
1. i = 0 => *(b+1) ie 4 is replaced by *(b+0)+5 ie 2+5
2. i = 1 => *(b+1) ie 4 is replaced by *(b+1)+5 ie 4+5
3. i = 2 => *(b+1) ie 4 is replaced by *(b+2)+5 ie 6+5
4. i = 3 => *(b+1) ie 4 is replaced by *(b+3)+5 ie 8+5
5. i = 4 => *(b+1) ie 4 is replaced by *(b+4)+5 ie 10+5
Nirlep said:
1 decade ago
But in *(a+1) it is not incremented.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers