Consider this example:
struct { int num; } s, *ps; s.num = 0; ps = &s; ++ps->num; printf('%d', s.num); /* Prints 1 */
It prints 1.
So I understand that it is because according to operators precedence, -> is higher than ++, so the value ps->num (which is 0) is firstly fetched and then the ++ operator operates on it, so it increments it to 1.
struct { int num; } s, *ps; s.num = 0; ps = &s; ps++->num; printf('%d', s.num); /* Prints 0 */
In this example I get 0 and I don’t understand why; the explanation of the first example should be the same for this example. But it seems that this expression is evaluated as follows:
At first, the operator ++ operates, and it operates on ps, so it increments it to the next struct. Only then -> operates and it does nothing because it just fetches the num field of the next struct and does nothing with it.
But it contradicts the precedence of operators, which says that -> have higher precedence than ++.
Can someone explain this behavior?
Edit:
After reading two answers which refer to a C++ precedence tables which indicate that a prefix ++/-- operators have lower precedence than ->, I did some googling and came up with this link that states that this rule applies also to C itself. It fits exactly and fully explains this behavior, but I must add that the table in this link contradicts a table in my own copy of K&R ANSI C. So if you have suggestions as to which source is correct I would like to know.
Thanks.
The post-increment (
ps++) and the pre-increment (++ps) have different associativity in C. The former associates left-to-right whereas the latter associates right-to-left. Check this page out (though this is for C++, so the precedences may be misleading).In your last example you are changing the pointer to one past the end of
&s. You have not changed the value of the pointee. If you need to incrementnum, you need to bind the++tonum.Detailed explanation:
A (hypothetical) compiler on seeing this expression, may push the
psobject to the stack followed by the++operator, the->operator and finally the object —num. While evaluationg, where should the compiler start? It looks at the available operators i.e.++and->. Does it chooseps++orps? Here precedence rules: since->has a higher precedence than++, it takes->to process withnumas one operand andpsas the other operand. So, the value of the expression becomesps->numi.e. 0 as you rightly observe. What happens topsafter the evaluation? Remember, there is another operator left on the stack? So, the compiler applies this topsand it now points to one element past&s.Footnote:
The ANSI/ISO C standard does not use a operator precedence grammar. Rather it uses what is known as a fully-factored grammar. This typically involves an exacting grammar definition dotted with a number of non-terminals like ‘primary-expression’ and ‘shift-expression’ and so on. This is hard to understand, but is easier for the language designer or the compiler vendor. Also, such a grammar is able to encode precedences easily. However, any fully-factored grammar is compatible with an operator-precedence grammar and this is what most books (and websites) do (and also mess up at times).