fixing lvalue quired as increment operand

This is a discussion on fixing lvalue quired as increment operand within the C Programming forums, part of the General Programming Boards category; Hi, Compiler complained about the second line: error: lvalue required as increment operand Code: for (; n > 0; n--) ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    fixing lvalue quired as increment operand

    Hi,

    Compiler complained about the second line: error: lvalue required as increment operand
    Code:
    	for (; n > 0; n--) {
    	    target = *((int *)lines)++;
    	    new_line(target);
    	}
    I changed it to:

    Code:
            int *temp = (int*) lines;
    	for (; n > 0; n--) {
                target = *temp;
                temp = temp+1;
    	    new_line(target);
    	}
    Am I interpreting the sequence of operations of "target = *((int *)lines)++;" correctly?

    Thanks a lot for your help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    The result of a cast in C is an r-value, but the increment must work on an l-value.

    Some compilers (in some circumstances) relax this rule a little bit.
    L-Value and R-Value Expressions (C)

    My guess is your cast changed the size/alignment (I'm guessing lines isn't an int pointer/array).

    If lines is a char array for example, then you've got other issues to deal with, namely alignment.
    For example, if your char array begins at an odd address, and your machine can only access an int on an even address, then the result of your code will be an alignment exception.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Read the for loop syntax first from here

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mr.777 View Post
    Read the for loop syntax first from here
    There's nothing wrong with the loop syntax. You can omit any of the pieces of the for loop you wish to. They are all optional.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    So what's the initial value of n then???

    That's what i meant to recommend him/her to see syntax as initialization of variable is must with some value....

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So what's the initial value of n then???
    It's whatever the previous value.


    That's what i meant to recommend him/her to see syntax as initialization of variable is must with some value....
    Not necessarily.

    say if i have
    Code:
    n = 0;
    while( ... ) {
       n = ...
    }
    if( something ) {   // continue use n
      for(; n > 0 ;n-- ) {
    }
    }

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Huh......
    Okay, think that, n is only declared but not initialized, then what?
    Do you really think, his program will work fine?
    And rather fighting with each other, concentrate to solve his/her problem....:-)
    Peace out..

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Okay, think that, n is only declared but not initialized, then what?
    Do you really think, his program will work fine?
    We don't know whether it's init or not.
    Anyway OP question has nothing to do with it.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Well the OP was asking about cast expressions and l-values, not the finer points on the use of a for loop.

    If you're still stuck on for loops, perhaps start your own thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    sorry to cause confusion. I didn't show the complete code. n is predefined. lines is void*.

    yes, I want to make sure that I am interpreting the lvalue rvalue stuff correctly. The loop is irrelevant to the discussion. Thanks.

    Quote Originally Posted by Salem View Post
    Well the OP was asking about cast expressions and l-values, not the finer points on the use of a for loop.

    If you're still stuck on for loops, perhaps start your own thread.
    Last edited by candy.chiu.ad; 03-07-2011 at 01:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lvalue required as increment operand
    By .C-Man. in forum C Programming
    Replies: 4
    Last Post: 10-13-2010, 02:41 PM
  2. classes and objects
    By GoldenBoy in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2010, 07:28 AM
  3. lvalue required as increment operand compile error
    By canadatom in forum C Programming
    Replies: 8
    Last Post: 06-13-2009, 11:49 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

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