No.The value of 11 is used because the value of the expression ptr++
is the value of ptr
before it is incremented. Full stop. No sequence point here!
A sequence point does happen before the function is entered in the function call and the relevance of sequence points is that the increment of ptr must have taken place before the function is entered.
Here's code that shows that happening:
#include <stdio.h>
int arr[] = {11, 22, 33};
int *ptr = arr;
int indirect(const char *fmt,int val){
printf("peek: %p (%d)\n",ptr,*ptr);
return printf(fmt,val);
}
int main(void)
{
printf("ptr at: %p\n", (void *)ptr);
indirect("Result of expression: %d\n", *ptr++); // line 8
printf("ptr at: %p\n", (void *)ptr);
}
Typical output:
ptr at: 0x2ac98105b030
peek: 0x2ac98105b034 (22)
Result of expression: 11
ptr at: 0x2ac98105b034
The rules about sequence points are telling you that the second must show ptr
incremented and must have (22)
on the end.
It would violate the function call sequence point rule if we saw peek: 0x2ac98105b030 (11)
but ptr at: 0x2ac98105b034
in the last line.
That would mean the side-effect (i.e. increment of ptr
) had taken place after the entry to indirect()
.
Precedence defines the order of evaluation but sequence points specify points before which side-effects must have taken place.
A good way to reason about them is to separate the concerns of evaluation and side-effect. Go through evaluating expressions and write down all the side-effects and then look at sequence point rules that tell you when they must have taken place before.
If you do that here (key steps in line 8):
- Take the value of ptr.
- Note: Also increment ptr by 1 (
int
).
- Determine the value stored at the value of
ptr
taken in step 1.
- Call
printf()
.
Step 4 is a sequence point so we must make sure step 2 happens before 4.
But it could happen before 3.
it's easy to get confused in C because the pre- and post-increment operators seen to have sequencing in their definition.
Think of p++ as "take the value of p and side-effect increment p".
Think of ++p as "take the value of p+1 and side-effect increment p" not as "increment p and take its value".
That second phrasing for ++p
implies a sequencing that the standard doesn't guarantee and while may take place is definitely not guaranteed.