Bug 6765 - gcc - incrementation - ternary operator
Summary: gcc - incrementation - ternary operator
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2002-05-22 04:26 UTC by jompo
Modified: 2005-11-10 03:49 UTC (History)
43 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jompo 2002-05-22 04:26:01 UTC
the ternary operator causes incrementation of left hand side expression to occurs before it should in an expression where the variable appears at both side of the affectation expression.
example: *s++ = (*s == 'x') ?  'X': *s;
Note: this does not appears in gcc-2.96 (Mandrake 8.2)

Release:
gcc 2.95.2 - gcc 2.95.3 - gcc 3.04

Environment:
gcc 2.95.3, gcc 3.04 (home compiled on Linux Mandrake 8.1)
Mac OS X : gcc 2.95.2

How-To-Repeat:
#include <stdio.h>
#define STRING "abc"
int main()
{
   char s[10], *t;

   strcpy(s, STRING);
   t = s;
   while (*t != '\0')
    *t++ = (*t == 'b') ? 'X': *t;
 
        /*  this print "Xc" instead of "aXc"  */
   printf("%s\n",s);
}
Comment 1 jompo 2002-05-22 04:26:01 UTC
Fix:
Must replace ternary operator by if/else instruction
Comment 2 Neil Booth 2002-05-22 05:03:03 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug. The standard states that whether the LHS or the RHS of '=' is evaluated first is undefined.  You were lucky before.
Comment 3 Andrew Pinski 2005-11-10 03:48:57 UTC
Reopening to ...
Comment 4 Andrew Pinski 2005-11-10 03:49:06 UTC
a dup of bug 11751

*** This bug has been marked as a duplicate of 11751 ***