0

I was trying to understand pointers in C and was trying a code sample, which gave an absurd result.

Following is the code sample :

void main()
{
    int *a;
    int arr[5] = {1,3,4,5,2};
    printf("%d\n", *(arr+1));
    a = arr;
    printf("Address: %p || Value: %d\n", ++a, *a);
}

okay so I expected it to output the address and value of second index of the array but unfortunately the output value isn't not equal to 3 (the second index value of the array) as opposed to the address which comes out correct.

The problem here is that the prefix ++a expression doesn't update the pointer variable a and when later in the printf function a is dereferenced the updated value is not used and hence the output shows the first index value of the array.

Please help me on this.

NoSuchUserException
  • 700
  • 1
  • 9
  • 18
  • This absurd behavior is caused by undefined behavior of your program. – haccks Aug 14 '18 at 10:01
  • There are many things a Java programmer needs to unlearn when learning C or C++. That function arguments are evaluated in a particular order is one of them. – molbdnilo Aug 14 '18 at 10:07
  • @molbdnilo In C, the function arguments are not evaluated in a specified order. – chux - Reinstate Monica Aug 14 '18 at 10:12
  • @chux function arguments aren't evaluated in a particular order, but that is fundamental right? or not? – NoSuchUserException Aug 14 '18 at 10:14
  • @haccks okay let me understand it the right way, here the absurd behaviour is caused by the arguments not evaluating in a specified order, right? – NoSuchUserException Aug 14 '18 at 10:16
  • @mohitR0_0; Yes. you got it right. – haccks Aug 14 '18 at 10:18
  • @haccks okay but the post you referred talks about the constructs related to only unary '++' and not function arguments undefined behaviour, won't it be fine to just not tag it as duplicate since it's not the same. – NoSuchUserException Aug 14 '18 at 10:22
  • it us because the `*a` is passed earlier than the `++a` and it is implementation defined. – 0___________ Aug 14 '18 at 10:24
  • @chux Exactly. Hence, it is something that you need to unlearn. – molbdnilo Aug 14 '18 at 10:27
  • @P__J__ is it correct to say that in this particular implementation the arguments to a function is always passed in a right to left order? – NoSuchUserException Aug 14 '18 at 10:30
  • You cant relay on it. Just move operations outside the function call. – 0___________ Aug 14 '18 at 10:32
  • yes that should be the correct practice I guess – NoSuchUserException Aug 14 '18 at 10:33
  • @mohitR0_0: The problem goes beyond evaluation order - attempting to modify the value of an object (`++a`) and then use that value of that object in a computation (`*a`) without an intervening sequence point is explicitly called out as *undefined behavior* by the language definition, and the result of undefined behavior can be quite literally anything. It's a coding error, period, regardless of how the compiler orders the operations. – John Bode Aug 14 '18 at 14:18

0 Answers0