Thread: ternary operator

  1. #31
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't know what kind of compiler you're using:
    Code:
    itsme@itsme:~/C$ cat cond.c
    #include <stdio.h>
    
    int main(void)
    {
      int a = 0;
      int b;
    
      a<10?b=100:b=200;
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ gcc -Wall cond.c -o cond
    cond.c: In function `main':
    cond.c:8: warning: use of conditional expressions as lvalues is deprecated
    cond.c:8: error: invalid lvalue in assignment
    itsme@itsme:~/C$
    It doesn't work no matter what a starts out as.
    If you understand what you're doing, you're not learning anything.

  2. #32
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    i am using Fedora core 5 gcc 4.1

    Guess i ll have to read up on the gcc...

    thanks for help everybody...

  3. #33
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by quzah
    Actually you're wrong. It ends up:

    (a < 10) = ...
    0 = ...
    or
    1 = ...

    That's why it's wrong. Because "a < 10" evaluates to either a 1 or a 0, and you're trying to assign a value to that. Which you can't. Because that's not a valid lvalue.


    Quzah.
    I'm sorry but that makes no sense to me. A conditional evaluates to either of it's two posible conditions. And the conditional is called before assignment opperator.

    The reason it returns an invalid lvalue, is because it is returning the value contained in b, not b itself. Thus you cannot derefference the value.

    It's still 200=... or the previous value of b = ...

    PS, if you use a?b=200:b=100, it still won't work.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #34
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by citizen
    While itsme's explaination is easier to understand but goes the wrong way, it is still correct.
    1. Evaluate c - d. if that is nonzero (true) then e() is called if a + b is true. If c - d is zero (false) call i() if a + b is true.
    2. Evaluate a + b. if it is true, then step 1 matters. If it's false, goto 3.
    3. Evaluate j * k. if it is true run f(), otherwise run g().
    Code:
    #include <stdio.h>
    
    int f(void) { return putchar('f'); }
    int g(void) { return putchar('g'); }
    int e(void) { return putchar('e'); }
    int i(void) { return putchar('i'); }
    
    int main(void) {
       int a = 2, b = 3;
       int c = 1, d = -1;
       int j = 0, k = 5;
    
       (a + b)? (c - d)? e() : i() : (j * k)? f() : g();
    
       return 0;
    }
    Playing with that makes it pretty clear.
    Trouble with testing it is that it does not tell you what is undefined. Does c-d get called before j*k? always or just on my compiler?

    But perhalps I should run a few tests. It will atleast tell me which functions and opperations do not get called.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #35
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    Quote Originally Posted by abhijith gopal

    2. correct me if i am wrong..

    this is my interpretation of the code
    (a<10?b=100:b)=200;

    the value of "b" is returned... now since there is an intializing statement the value 200 is equated against the value of b which was returned previously...
    I'm sorry but that makes no sense to me. A conditional evaluates to either of it's two posible conditions. And the conditional is called before assignment opperator.

    The reason it returns an invalid lvalue, is because it is returning the value contained in b, not b itself. Thus you cannot derefference the value.

    It's still 200=... or the previous value of b = ...

    PS, if you use a?b=200:b=100, it still won't work.
    Guess i was right according to King Mir's Explanation...

    but the statement does NOT flag an error when the second operand is not enclosed within parenthesis.. ie., a<10?b=100:(b =200); // for a less than ten
    Last edited by abhijith gopal; 07-10-2006 at 10:52 AM.

  6. #36
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Well, quzah seems to disagree with me, so give him a chance to reply before you take my word on it.

    In either case, a<10?b=100:(b =200) is perfectly valid, because the result of the conditional is not used as an lvalue for the assignment opperator.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #37
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Does c-d get called before j*k? always or just on my compiler?
    Always. This is because, on every operator table ever written, the ternary operator is associated right to left, and c - d is the rightmost operator. j * k is actually the else clause in the a + b evaluation, so it will always be evaluated last.

  8. #38
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by citizen
    Always. This is because, on every operator table ever written, the ternary operator is associated right to left, and c - d is the rightmost operator. j * k is actually the else clause in the a + b evaluation, so it will always be evaluated last.
    I get it now!!!

    But you're wrong. a + b gets evaluated first.
    Then either "(c - d)? e() : i()" or "(j * k)? f() : g()"

    Notice how "(c - d)? e() : i()?f():g()" is never evaluated. That's what is ment by the operator being read right to left.

    And all it takes is a useless example of "a?b?c:d:e" to get me all confused. That example does not show anything.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. ternary operator(?), vectors, and recursive function
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2003, 03:19 PM