Thread: quick syntax question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In effect, yes. Alternatively:
    Code:
    *(s->top) = c;
    (s->top)++;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    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.
    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

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    Ixnay. The post-increment will occur before the assignment.
    The post-increment does not happen until after s->top has been fully evaluated. Whether it occurs before or after the assignment does not change the result.

    The line of code stores 'c' into the location pointed to by s->top, then increments s->top.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    The post-increment does not happen until after s->top has been fully evaluated. Whether it occurs before or after the assignment does not change the result.

    The line of code stores 'c' into the location pointed to by s->top, then increments s->top.
    Sorry -- I was looking at this order of precedence table:

    C Operator Precedence Table

    However, it's obviously wrong as even the simple:
    Code:
    int a = 5, b = a++;
    demonstrates (which if it were my code, I'd have been writing it and noticed )
    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

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    Sorry -- I was looking at this order of precedence table:

    C Operator Precedence Table

    However, it's obviously wrong as even the simple:
    Code:
    int a = 5, b = a++;
    demonstrates (which if it were my code, I'd have been writing it and noticed )
    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);
    //}

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    Well that is very deceptive then isn't it
    In most cases, binding order completely defines order of operations. It's just that it doesn't cover every case. Even expressions that don't involve increment/decrement operators can have ambiguous evaluation order:

    Code:
    x = a * b + c * d;
    You can't tell whether a * b will evaluate before, or after, c * d. Left-to-right associativity doesn't factor in, because of the intervening addition. Even parenthesizing the expression doesn't remove the ambiguity. The only way to force a particular order is to insert a sequence point by splitting across more than one statement.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM