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.
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;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Last edited by MK27; 12-21-2009 at 11:07 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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?
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
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!)Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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)++;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
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++;
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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: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.
Last edited by laserlight; 12-21-2009 at 01:34 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Well that is very deceptive then isn't it
I guess the best means of resolving uncertainty is still actual experimentation.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge