Using i++ inside ternary statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Using i++ inside ternary statement?

    Hello - I have seen many posts and faq's about the warning "operation on i may not be defined", which explain that it is coming up due to sequence points and compiler optimization.

    But - how does one fix that problem within a ternary statement, properly??

    I have a statement:

    Code:
    a[i++] = funct1( (cond1? &a[1] : &a[0] ), (i-1) );
    Should I break this up into multiple statements? Or what is the best way to approach working with such a statement?

    thanks!!
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Should I break this up into multiple statements?
    One part of it will have to be broken up into multiple statements. You have a[i++]. Make it a[i]. Then explicitly increment i afterwards.

    i++ does not mean use i for the entire statement, then increment i afterwards. You can't actually guarantee exactly where during the statement it will be incremented, thus making the entire statement undefined. If you have read the material on sequence points, this should make sense to you.

    The ternary operation is not the problem.

    Comment

    Working...