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 1 of 6.
Tash said:
7 years ago
@All.
According to me, the processed output is;
BEFORE *(b+1) = *(b+i)+5;==>> i=0 --- *(b+1)== 4 &&&&& *(b+i) ie *(b+0)== 2,
AFTER *(b+1) = *(b+i)+5; ==>> i=0 --- *(b+1)== 7 &&&&& *(b+i) ie *(b+0)== 2,
BEFORE *(b+1) = *(b+i)+5;==>> i=1 --- *(b+1)== 7 &&&&& *(b+i) ie *(b+1)== 7,
AFTER *(b+1) = *(b+i)+5; ==>> i=1 --- *(b+1)== 12 &&&&& *(b+i) ie *(b+1)== 12,
BEFORE *(b+1) = *(b+i)+5;==>> i=2 --- *(b+1)== 12 &&&&& *(b+i) ie *(b+2)== 6,
AFTER *(b+1) = *(b+i)+5; ==>> i=2 --- *(b+1)== 11 &&&&& *(b+i) ie *(b+2)== 6,
BEFORE *(b+1) = *(b+i)+5;==>> i=3 --- *(b+1)== 11 &&&&& *(b+i) ie *(b+3)== 8,
AFTER *(b+1) = *(b+i)+5; ==>> i=3 --- *(b+1)== 13 &&&&& *(b+i) ie *(b+3)== 8,
BEFORE *(b+1) = *(b+i)+5;==>> i=4 --- *(b+1)== 13 &&&&& *(b+i) ie *(b+4)== 10,
AFTER *(b+1) = *(b+i)+5; ==>> i=4 --- *(b+1)== 15 &&&&& *(b+i) ie *(b+4)== 10,
========================================================== OUTPUT:
2, 15, 6, 8, 10,
==========================================================
Only the value of *(b+1) ie b[1] ==4 is changing in memory till the end of the loop.
According to me, the processed output is;
BEFORE *(b+1) = *(b+i)+5;==>> i=0 --- *(b+1)== 4 &&&&& *(b+i) ie *(b+0)== 2,
AFTER *(b+1) = *(b+i)+5; ==>> i=0 --- *(b+1)== 7 &&&&& *(b+i) ie *(b+0)== 2,
BEFORE *(b+1) = *(b+i)+5;==>> i=1 --- *(b+1)== 7 &&&&& *(b+i) ie *(b+1)== 7,
AFTER *(b+1) = *(b+i)+5; ==>> i=1 --- *(b+1)== 12 &&&&& *(b+i) ie *(b+1)== 12,
BEFORE *(b+1) = *(b+i)+5;==>> i=2 --- *(b+1)== 12 &&&&& *(b+i) ie *(b+2)== 6,
AFTER *(b+1) = *(b+i)+5; ==>> i=2 --- *(b+1)== 11 &&&&& *(b+i) ie *(b+2)== 6,
BEFORE *(b+1) = *(b+i)+5;==>> i=3 --- *(b+1)== 11 &&&&& *(b+i) ie *(b+3)== 8,
AFTER *(b+1) = *(b+i)+5; ==>> i=3 --- *(b+1)== 13 &&&&& *(b+i) ie *(b+3)== 8,
BEFORE *(b+1) = *(b+i)+5;==>> i=4 --- *(b+1)== 13 &&&&& *(b+i) ie *(b+4)== 10,
AFTER *(b+1) = *(b+i)+5; ==>> i=4 --- *(b+1)== 15 &&&&& *(b+i) ie *(b+4)== 10,
========================================================== OUTPUT:
2, 15, 6, 8, 10,
==========================================================
Only the value of *(b+1) ie b[1] ==4 is changing in memory till the end of the loop.
(5)
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[].
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.
Sowmiya said:
7 years ago
for(i=0;i<n;i++)
*(b+1)=*(b+i)+5;
Array={2,4,6,8,10};
*(b+1) indicates value at 0+1th index,that is value at array[1] becomes(4 becomes),b always be zero at all iteration,
i=0,In 1st iteration-2+5=7,
i=1,In 2 iteration-4+5=9,
i=2,In 3 iteration-6+5=11,
i=3,In 4 iteration-8+5=12,
i=4,In 5 iteration-10+5=15.
In these all iterations, only the value at array[1] changes at every iteration.
*(b+1)=*(b+i)+5;
Array={2,4,6,8,10};
*(b+1) indicates value at 0+1th index,that is value at array[1] becomes(4 becomes),b always be zero at all iteration,
i=0,In 1st iteration-2+5=7,
i=1,In 2 iteration-4+5=9,
i=2,In 3 iteration-6+5=11,
i=3,In 4 iteration-8+5=12,
i=4,In 5 iteration-10+5=15.
In these all iterations, only the value at array[1] changes at every iteration.
(2)
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
A H jafri said:
8 years 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) i.e 4 is replaced by *(b+1)+5 ie 4+5
3. i = 2 => *(b+1) i.e 9 is replaced by *(b+2)+5 ie 6+5
4. i = 3 => *(b+1) i.e 11 is replaced by *(b+3)+5 ie 8+5
5. i = 4 => *(b+1) i.e 15 is replaced by *(b+4)+5 ie 10+5
So, finally, 4 is replaced by 15 remaining are same.
1. i = 0 => *(b+1) ie 4 is replaced by *(b+0)+5 ie 2+5
2. i = 1 => *(b+1) i.e 4 is replaced by *(b+1)+5 ie 4+5
3. i = 2 => *(b+1) i.e 9 is replaced by *(b+2)+5 ie 6+5
4. i = 3 => *(b+1) i.e 11 is replaced by *(b+3)+5 ie 8+5
5. i = 4 => *(b+1) i.e 15 is replaced by *(b+4)+5 ie 10+5
So, finally, 4 is replaced by 15 remaining are same.
(3)
Vijay said:
3 years ago
#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;
}
Here the entire sub function work is to change second value in the array .
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;
}
Here the entire sub function work is to change second value in the array .
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers