Thread: Lvalue required as increment operand

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    79

    Lvalue required as increment operand

    Code:
    #include <stdio.h>
    
    int main(void) {
    	
    	int iVar; //line 1
    	
    	iVar = 20; //line 2
    	
    	++iVar; //line 3
    	
    	printf( "\niVar : %d\n", iVar ); // line 4
    	
    	iVar--; // line 5
    	
    	printf( "\niVar : %d\n", iVar ); // line 6
    	
    	iVar + ++iVar--; //line 7
    	
    	printf( "\niVar : %d\n", iVar ); // line 8
    	
    	++( iVar + iVar ); // line 9
    	
    	printf( "\niVar : %d\n", iVar ); // line 10
    	
    	return 0; // line 11
    }
    What's the problem?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly are you trying to do with these two statements?
    Code:
    iVar + ++iVar--;
    Code:
    ++( iVar + iVar );
    The error message is basically saying that you are trying to increment something that cannot be assigned to, e.g., it is the temporary that resulted from another operation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    This code is pretty much simple arithmetic since my teacher is trying to teach us how to use operators. I don't get why it doesn't add together... Do those statements not work at all?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by .C-Man.
    This code is pretty much simple arithmetic since my teacher is trying to teach us how to use operators. I don't get why it doesn't add together... Do those statements not work at all?
    They do not work because the increment and decrement operators change the value of their operands. Hence, it is futile to try and use them on things that cannot change, including the result of operations like addition. It is akin to adding 3 and 4 to get 7, adding 1, and then trying to assign 8 to 7.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    That really helps, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and objects
    By GoldenBoy in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2010, 07:28 AM
  2. Replies: 3
    Last Post: 06-01-2010, 06:22 AM
  3. lvalue required as left operand of assignment (??)
    By oospill in forum C Programming
    Replies: 3
    Last Post: 11-03-2009, 11:02 PM
  4. lvalue required as increment operand compile error
    By canadatom in forum C Programming
    Replies: 8
    Last Post: 06-13-2009, 11:49 AM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM