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;
}
7, 9, 11, 13, 15
2, 15, 6, 8, 10
2 4 6 8 10
3, 1, -1, -3, -5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
52 comments Page 5 of 6.

Devendar said:   8 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.

Nakul said:   8 years ago
What is the prototype of change function?

Suraj said:   8 years ago
The answer is B @Devendar.

Mrinal said:   8 years ago
How the first element printed is 2?

PRATHYUSHA said:   8 years ago
Thank you @Pankaj Singh.

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.
(3)

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.

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.
(2)

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.
(5)

Vaibhav said:   6 years ago
Why? Please explain.


Post your comments here:

Your comments will be displayed after verification.