Search code examples
coperator-keywordternary

proper use of the ternary operator in a function strToInt (C)


for (minus == false ? i = 0 : i = 1; string[i] >= '0' && string[i] <= '9'; ++i)
    {
        intValue = string[i] - '0';

        minus == false ? result = result * 10 + intValue : 
            result = result * 10 - intValue;

    }

error: expression is not assignable screenshot - http://share.pho.to/AarcJ

https://codeshare.io/5Pdd7X


Solution

  • Use (for example)

    for (i = minus? 1:0; string[i].... etc
    

    And...

    result = result * 10 + minus? (-lastvalue) : lastvalue;