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.
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 .
Saif said:
5 years ago
The function isn't declared and defined after the main shouldn't be Wrong? Please explain.
Vaibhav said:
6 years ago
Why? Please explain.
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)
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)
Mothi said:
8 years ago
Change (a, 5); run and then the control is given back to the main function not stored. (i. E) it cannot update any value. So it prints the value of local variable.
Hence the output is 2, 4, 6, 8, 10.
Hence the output is 2, 4, 6, 8, 10.
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)
PRATHYUSHA said:
8 years ago
Thank you @Pankaj Singh.
Mrinal said:
8 years ago
How the first element printed is 2?
Suraj said:
8 years ago
The answer is B @Devendar.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers