can someone explain to me what's happening here plz?
I know s is a pointer to a struct, so *(s->top) is the top value of the struct s points to.Code:*(s->top)++=c;
I don't understand what ++= means though.
Printable View
can someone explain to me what's happening here plz?
I know s is a pointer to a struct, so *(s->top) is the top value of the struct s points to.Code:*(s->top)++=c;
I don't understand what ++= means though.
Space it out a little and it might become more obvious:
Code:*(s->top)++ = c;
It adds one to the position of the pointer (s->top), altho I think it would be better written as:
*((s->top)++)) = c;
since it might instead add one to the value of s->top (which is pointless in an assignment).
I'd have to do a quick experiment to tell the difference.
Dont you think a * with (s->top) is illegal coz the value of top will be given by s->top if top is a member of struct having pointer s?
I feel that if you think that *p++ might be misconstrued as (*p)++, then you should break it up instead of trying to clarify with parentheses. The parentheses spoil the fun of having such a condensed expression, in my opinion :) (so... *s->top++=c; FTW!)Quote:
Originally Posted by MK27
so if I get it right *(s->top)++=c; equals
?Code:int *ptr = s->top;
*ptr=c;
ptr++;
s->top=ptr;
In effect, yes. Alternatively:
Code:*(s->top) = c;
(s->top)++;
Ixnay. The post-increment will occur before the assignment. If the point is to add one to the value of s->top, the ++ does nothing in the end.
If the point is to make s->top point to what was the equivalent of s->top[1], then that space better be part of s, because it will be set to the value of c.
Sorry -- I was looking at this order of precedence table:
C Operator Precedence Table
However, it's obviously wrong as even the simple:
demonstrates (which if it were my code, I'd have been writing it and noticed ;) )Code:int a = 5, b = a++;
hmm... I sent you this in my PM, but anyway...
It is true that postfix increment operator has a higher precedence than the copy assignment operator. But what that means is that this:Quote:
Originally Posted by MK27
should be interpreted as:Code:a = b++;
rather than:Code:a = (b++);
whereas this:Code:(a = b)++;
should be interpreted as:Code:a = b, c;
not:Code:(a = b), c;
since the copy assignment operator has a higher precedence than the comma operator.Code:a = (b, c);
EDIT:
Perhaps you are missing a fundamental point: the result of a++ is the value of a before the increment.
Precedence controls order of binding, not order of operations. It specifies which operations will occur to which expressions, not when they will occur. The order of operations is controlled by associativity and sequence points. Operators in different precedence classes are treated independently when determining associativity.
On top of all that, the compiler can evaluate the expression any way it likes, so long as the result is the same as if it had done it in the order defined by the language.
The code in question looks like it's pushing something onto a stack.
Well that is very deceptive then isn't it :p
I guess the best means of resolving uncertainty is still actual experimentation.