Mark said:
foo1 is global object and thus all members are initialized to 0.
Ok. That wasn't clear from your original post; the declaration of foo1
appeared to be at the same scope as the printf call.
I expected to have member 'a' of teh structure 'struct foo' to be
incremented, i.e. 'foo1.a' yields a current value of 'a' and then I
increment the value -- this was my expectation. Where is my reasoning wrong
?
But in my macro I have parentheses around, i.e. (foo1.a)++.
The parentheses aren't the point; Fred was only talking about foo1 being
uninitialized (which apparerently is not an issue).
You wrote, "Also FOO_A(foo1)++ doesn't seem to work, I don't understand
why". You should be aware that that's nearly the least useful statement
you could make. You didn't tell us what it did, or what you expected it
to do, or how those differed. Fred asked:
What were the actual results, and how were they different than what
you expected?
You never answered.
Here's what I *think* is going on. You write:
printf("-> %d %d\n", foo1.a, FOO_A(foo1)++);
and it worked exactly as it should, but you thought it should behave
differently. As Fred said, that's exactly equivalent to:
printf("-> %d %d\n", foo1.a, foo1.a++);
Since the initial value of foo.a is 0 (that's new information),
"foo1.a++" will have the side effect of incrementing foo.a, setting it
to 1. But the result of the postfix "++" operator is the value of the
operand *before* it's incremented, so the value printed will be 0. (If
that weren't the case, there would be no point in having prefix and
postfix versions of "++"; they'd both do the same thing.)
But there is another problem, and as Fred points out the behavior of
printf("-> %d %d\n", foo1.a, foo1.a++);
is undefined. It reads and modifies foo.a twice without an
intervening sequence point (in the second and third arguments,
respectively), and the value it reads isn't used to determine the
value to be written. The side effect of the increment could happen
either before or after the evaluation of the second argument.
Rather than just permitting evaluation to occur in either order,
the standard makes the behavior completely undefined; literally
anything could happen.
You can fix this by splitting it into two printf calls:
printf("--> %d", foo1.a);
printf(" %d\n", foo1.a++);