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 3 of 6.

@ashwini said:   1 decade ago
Hey guys i din understand... confused...
If after computin *(b+1)=*(b+i)+5;
we get 7,12,11,13,15
then how cum is OPTION B the answer...
Please help m out!!

Mahesh said:   8 years ago
1. The problem is in the form of call by value. So it has to print in called method. But in the given problem, printf statement was in the calling method.

Pratik patel said:   1 decade ago
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5; /*( b[1]=b[i]+5 ) . */

End time loop.
b[1] = b[5]+5.
b[1] = 10+5.
b[1] = 15.

Ans = 2, 15, 6, 8, 10.

Loknath said:   1 decade ago
Hello friends
*(b+1)=b[1];
for(i=0;i<n;i++)
b[1]=b[i]+5;
//at last b[1]=2+5......at last b[1]=10+5;
then store in address b[1]=15
ok good day

Ashishprabhakar said:   1 decade ago
Here in 2nd last line clearly mentioned
*(b+1) = *(b+i)+5;
that means only *(b+1)will change all other will remain as it is....

Vignesh said:   1 decade ago
Answer must be option a function definition is perform and return the value to the function call so it should be a option (A).

Mahesh said:   8 years ago
According to the logic, answer should be 7, 9, 11, 13, 15. But answer shows 2, 15, 6, 8.

How it is correct?

Anitha said:   8 years ago
I understood the concept.

But why do we change the second element alone? Please anyone describe it for me.

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:

Your comments will be displayed after verification.