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:
53 comments Page 1 of 6.
Tash said:
8 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)
A H jafri said:
9 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)
Sowmiya said:
8 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)
Robert said:
1 decade 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)
Devendar said:
9 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.
Manoj said:
1 decade ago
Change the position of a[i] with respect to the position of the second element of the array.
Pankaj Singh said:
1 decade ago
*[b+1] = *[b+i]+5;
*[a+1] = *[b+0]+5;
a[1] = b[0]+5;
a[1] = 2+5;
a[1] = 7;
Now it will continuous till the b[i] = 10.
So a[1] = 15;
For other it will remain same.
2 15 6 8 10.
a[0] a[1] a[2] a[3] a[4].
*[a+1] = *[b+0]+5;
a[1] = b[0]+5;
a[1] = 2+5;
a[1] = 7;
Now it will continuous till the b[i] = 10.
So a[1] = 15;
For other it will remain same.
2 15 6 8 10.
a[0] a[1] a[2] a[3] a[4].
Sujju said:
1 decade ago
#include<stdio.h>
int main(){
int a=5,b=10,c=15;
int *arr[]={&a,&b,&c};
printf("%d",*arr[1]);
return 0;
What is the o/p of above program?
int main(){
int a=5,b=10,c=15;
int *arr[]={&a,&b,&c};
printf("%d",*arr[1]);
return 0;
What is the o/p of above program?
Prashant kumar said:
1 decade ago
Everything is fine for option B, but function "change" is not declared it will show error I think!
Vinoth said:
1 decade ago
But my question void means it does not return anything so how the values pass in to main function?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers