expression evaluation

This is a discussion on expression evaluation within the C Programming forums, part of the General Programming Boards category; Hi, Although the expression *((a<20)?&b:&c)=30; works but ((a<20)?b:c)=30 gives error: invalid lvalue in assignment. what could be the reason for ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    5

    Question expression evaluation

    Hi,


    Although the expression *((a<20)?&b:&c)=30; works but

    ((a<20)?b:c)=30 gives

    error: invalid lvalue in assignment.

    what could be the reason for it? Please need sum urgent help.

    Thanks in advance.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    The result of the left hand side of:
    Code:
    ((a<20)?b:c)=30
    is a value (the value stored in either b or c). Let's say for example that b is 20 and c is 30. If a is 10 then the result of the left-hand side is 20 and you are effectively trying to say:
    Code:
    20=30
    which would of course give you that error.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    5

    Question Help in assigment stmt.

    full program:
    Code:
    main()
    {
      int a=10,b,c;
      ((a<20)?b:c)=30;
      printf("%d",b);
     }
    this also gives error invalid lvalue in assignment.

    but if i replace the line with *((a<20)?&b:&c)=30; it works.

    please explain also why the second one works .

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by lazy_hack
    but if i replace the line with *((a<20)?&b:&c)=30; it works.

    please explain also why the second one works .
    You are assigning to what the corresponding pointer points to, and that is not a problem. Where did you come across this code, anyway? It looks like a lazy hack, or rather, bad style.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by lazy_hack View Post
    full program:
    Code:
    main()
    {
      int a=10,b,c;
      ((a<20)?b:c)=30;
      printf("%d",b);
     }
    this also gives error invalid lvalue in assignment.

    but if i replace the line with *((a<20)?&b:&c)=30; it works.

    please explain also why the second one works .
    In the code above b and c are having garbage values, that's why as told to you in post#2, it'll give you error, while using & you are directly "attacking" in the address, thus no error.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by BEN10
    In the code above b and c are having garbage values, that's why as told to you in post#2, it'll give you error, while using & you are directly "attacking" in the address, thus no error.
    That is off the mark. That b and c were not initialised does not matter, since the intention is to assign a value to them. What matters is that the result of the conditional operator is not an lvalue, hence the attempted assignment does not work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I think hk_mp5kpdw's post is also wrong then.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by BEN10
    I think hk_mp5kpdw's post is also wrong then.
    What do you find wrong about it? hk_mp5kpdw made the point that the result of the conditional operator is not a variable that can be assigned to, but a value that cannot be assigned to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    From hk_mp5kpdw's post, let b has a value of 12345 in the code given by the OP, then this will happen
    Code:
    12345=30;
    That's why the error. This is what I said in my previous post.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by BEN10 View Post
    In the code above b and c are having garbage values, that's why as told to you in post#2, it'll give you error, while using & you are directly "attacking" in the address, thus no error.
    No you said this. But as laserlight pointed out that the uninitialized b and c are not a problem here. The problem is the assignment being checked

    for example as in your case

    12345 = 30 // this is wrong

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by BEN10
    That's why the error. This is what I said in my previous post.
    Fair enough: looking at your post #5 again, I see that I missed the phrase "as told to you in post#2". It would have been clearer if you had given an example there and then, or at least suggested the scenario where 20 and 30 were the "garbage" values of b and c respectively.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    5
    thanks for the explanation...

    "the result of the conditional operator is not an lvalue" line is the crux i believe...

    the code was from test ur c skills book....just anther intrsting book..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21