Given this code:
typedef void (*Thunk)();
Thunk* gFP;
void foo(){ printf("Foo "); *gFP(); };
void bar(){ printf("Bar ");
Thunk Codex[] = { foo, bar };
gFP = Codex;
(*gFP++)();
Does the function call happen before or after the increment?
i.e: Will this print "Foo Foo Foo ..." or "Foo Bar"?
This is just my personal view. I'm not 100% convinced that this is correct. So, please forgive me if my answer is wrong.
C99 6.5.2.2/10 Function calls says:
The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
C99 6.5.2.4/2 Postfix increment and decrement operators says:
The side effect of updating the stored value of the operand shall occur between the previous and the next sequence point.
The side effect of post increment operator is completed somewhere before the
next sequence point.
Assuming the expression f( x ),
I think there is a sequence point after the evaluation of f and x, and
before the function call.
So, the side effect of gFP++ will be completed before the function call,
and the code in the question is expected to print Foo Bar.
Edit:
I removed the quotes from Annex-C in C99 and C++, and added the quotes from
C99.
Probably previous quotes were indistinct regarding the question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With