0

I am unable to understand the following code in С, using pointers

#include <stdio.h>
#include <stdlib.h>

int bags[5]={20,5,20,3,20};
int *next();

int main()
{
    int pos=5;
    *next()=pos;
    printf("%d,%d,%d",pos,*next(),bags[0]);
}

int *next()
{
    int i;
    for(i=0;i<5;i++)
    if(bags[i]==20)
        return(bags+i);
    printf("Error!");
}

Can anyone explain why the ans is 20,5,20.

6
  • 1
    Well, I ran this code and I got the ans as 5,20,5 Commented Apr 12, 2014 at 7:23
  • 1
    This code invokes undefined behaviour if the if condition fails in next function. Commented Apr 12, 2014 at 7:25
  • @ajay In theory. In practice it always returns bags+i since i = 0 on he first iteration. So the loop is useless, i=0 would do. Commented Apr 12, 2014 at 7:27
  • 1
    @jens First iteration yes.. future iterations no, as the values may have changed Commented Apr 12, 2014 at 7:32
  • @MattMcNabb I don't see any future iterations in the program as is. next() is called only once and bags never modified. Commented Apr 12, 2014 at 9:32

3 Answers 3

3

The output of the program will is,

5,20,5

because

if(bags[i]==20)
        return(bags+i);

returns pointer to bags[0] since bags[0]==20 and return is a pointer to it and

*next()=pos;

writes pos value to the address pointed by next() returned address, i.e. bags[0]=pos=5

Sign up to request clarification or add additional context in comments.

1 Comment

ya sorry the output will be 5,20,5.I was experimenting with the input data so mistakenly copied the wrong data. thanx a lot
0

The function next returns a pointer to the first array element equal to 20. So, *next()=pos; will modify the first element of the array from 20 to 5. Then it is simple to understand why the output is 5,20,5: since pos is five, *next() returns 20 and bags[0] is 5 as explained above.

Comments

0

The output is 5,20,5.

Note: int *next(); ==> next() is a function that returns int*.

[1] The call to next() from the line *next() = pos;. C evaluates the function from left so next() gets called first. Because of if (bags[i] == 20), the function next() returns bags+i which is simply bags since i is 0 at the moment. So next() returned the address of bags which means *next() is nothing but the pointer to bags[0]. So bags[0] becomes 5.

[2] When the execution comes to this line: printf("%d,%d,%d",pos,*next(),bags[0]);, it prints value of pos i.e 5, value of *next() which is 20 as next() returned the address of bags[0].

[3] *next() prints 20 because, this time next will return the address of bags[2] since bags[0] got assigned with the value of pos in the line before the printf().

[4] And finally, bags[0] is 5. See the explanation in [1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.