Thread: lvalue required as increment operand compile error

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    lvalue required as increment operand compile error

    Code:
    int
    
    read_itr_seq(void *buf, int itype, FILE *f)
    
    {
    
      int l, ch;
    
      void *p = buf ;
    
      if ( itype < 7 || (itype > 13 && itype != 20) ) return -2 ;
    
      l = (ch = getc(f)) ;
    
      if ( l == 0 ) {
    
        while ( (ch = getc(f)) == 0 ) ;
    
        ch = getc(f) ;		/* Second byte of chunk word */
    
        l = (ch = getc(f)) ;
    
      }
    
      else if ( ch == 0xff ) return 0 ;
    
      *((u_char *)p)++ = l ; 
    
      if ( itype < 11 || itype == 20 ) fread(p, (size_t)(l + 22), (size_t)1, f) ;
    
      else fread(p, (size_t)(l + 23), (size_t)1, f) ;
    
      if ( ferror(f) ) return -1 ;
    
      return 1 ;
    
    }
    Code:
      *((u_char *)p)++ = l ;
    has the compile error lvalue required as increment operand, I am using GCC4.3.3 and this code works under GCC 2.96, would someone give me a hint on this? Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm gonna guess that is better written as
    Code:
    *(p++) = l ;
    You'll have to test it yourself, but this is my reasoning:
    • the cast is unnecessary [edit] okay it is
    • the increment should be inside the parentheses, or else you are kind of saying add one to the dereferenced value, but then setting it (=)
    Last edited by MK27; 06-13-2009 at 11:11 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... you cast p to a u_char*, assign l to the u_char that this temporary pointer points to, then increment this temporary pointer. The increment is not useful, so it should suffice to write:
    Code:
    *(u_char *)p = l ;
    EDIT:
    Quote Originally Posted by MK27
    the cast is unnecessary
    Since p is a pointer to void, the cast is necessary in order to dereference the pointer and/or perform pointer arithmetic.

    EDIT #2:
    Quote Originally Posted by MK27
    the increment should be inside the parentheses, or else you are kind of saying add one to the dereferenced value, but then setting it (=)
    Actually, *(p++) is equivalent to *p++.
    Last edited by laserlight; 06-13-2009 at 11:20 AM.
    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

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    thanks for helping guys,

    *(u_char *)p = l; works for me,

    and
    Code:
    int
    
    extract_text_nosource(char *ip, int il, FIELD_TYPE field_type,
    
    		      Extraction_regime reg, int (*stemfn)(char *, int, char *),
    
    		      Gsl *gs, void *outptr, int outflag, int rec, int diag)
    
    {
    
      int l = il ;
    
      char *p = ip ;
    
      char *sp ;
    
      int pl, sl ;
    
      int nk ;
    
      int numterms = 0 ;
    
    
    
      int stopped ;
    
      int outlen ;
    
      void *opp = outptr ;
    
      while ( l > 0 ) {
    
        pl = find_para(p, l, INDENT|GAP) ; 
    
        stopped = 0 ;
    
        l -= pl ; sp = p ; p += pl ;
    
        while ( pl > 0 ) {
    
          sl = find_sentence(sp, pl, field_type, TEXTPOS_MAX_T, 0) ;
    
          nk = extract(sp, sl, reg, gs, stemfn, opp, outflag,
    
    		   rec, 0, 0, 0, (int *)NULL, &stopped, &outlen) ;
    
          if ( nk == -1 ) {
    
    	numterms = nk ;
    
    	break ;
    
          }
    
          pl -= sl ;
    
          sp += sl ;
    
          numterms += nk ;
    
          (char *)opp += outlen ;
    
        }
    
      }
    
      return numterms ;
    
    }
    Code:
    (char *)opp += outlen ;
    I got same errors here. I don't see anything wrong with this line.

    Thanks again for helping here
    Last edited by canadatom; 06-13-2009 at 11:29 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It's the same problem: from what I infer, the C standard now states that the result of a cast expression may not be used as an lvalue, i.e., it may not be modified. This makes sense, since the result of a cast expression is a temporary that would typically end its lifetime after the given statement.

    So, this statement:
    Code:
    (char *)opp += outlen ;
    causes a temporary pointer of type char* to be advanced by outlen. But this temporary is destroyed after this statement, so the statement has no net effect, were it legal in the first place.

    In this case, it looks like opp should just be a pointer to char to begin with.
    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

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    So make opp a char* and cast it to void where necessary.

    ps. they don't call you "Stompin'", do they?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    any hint for this line to be able to compile? Thanks

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by canadatom
    any hint for this line to be able to compile?
    Both MK27 and I have already given you such a suggestion.
    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

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    thanks a lot for helping : )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

Tags for this Thread