C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-13-2009, 10:56 AM   #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
canadatom is offline   Reply With Quote
Old 06-13-2009, 11:03 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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 (=)
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 06-13-2009 at 11:11 AM.
MK27 is offline   Reply With Quote
Old 06-13-2009, 11:05 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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++.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

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

Last edited by laserlight; 06-13-2009 at 11:20 AM.
laserlight is online now   Reply With Quote
Old 06-13-2009, 11:23 AM   #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.
canadatom is offline   Reply With Quote
Old 06-13-2009, 11:30 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 06-13-2009, 11:41 AM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
So make opp a char* and cast it to void where necessary.

ps. they don't call you "Stompin'", do they?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 06-13-2009, 11:42 AM   #7
Registered User
 
Join Date: Jun 2009
Posts: 4
any hint for this line to be able to compile? Thanks
canadatom is offline   Reply With Quote
Old 06-13-2009, 11:43 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 06-13-2009, 11:49 AM   #9
Registered User
 
Join Date: Jun 2009
Posts: 4
thanks a lot for helping : )
canadatom is offline   Reply With Quote
Reply

Tags
compile error, gcc, linux

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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