invalid lvalue in increment error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PatilRohit
    New Member
    • Jun 2020
    • 2

    invalid lvalue in increment error

    Code:
    /* after changing GCC version 3.0 to 4.1 i am getting invalid lvalue in increment error */
    
    #include <vscreen.h>
    #include "vscreen_internal.h"
    
    extern UDINT colPalette[256];
    
    
    void  memset_f(void *p,USINT value, UDINT len)
    {
           register	UDINT longValue = colPalette[value];
    
    	while(len)
    	{
    	if ( ((UDINT)p&3) == 0 )   /* even address*/
    	{
    	if (len > 32) /*and more than 32 bytes to fill */
    	{
    	*((UDINT*)p)++ = longValue;   /* error*/
            *((UDINT*)p)++ = longValue;   /* error*/
            *((UDINT*)p)++ = longValue;   /* error*/
            *((UDINT*)p)++ = longValue;   /* error*/
            *((UDINT*)p)++ = longValue;   /* error*/
            *((UDINT*)p)++ = longValue;   /* error*/
            *((UDINT*)p)++ = longValue;   /* error*/
            *((UDINT*)p)++ = longValue;   /* error*/
    	len-=32;
            continue;
    				
    	}
    
       }   
    
             *(USINT*)p++ = (USINT)longValue;  /* error*/
             len--;
    	}
    }
    
    #endif
    Last edited by gits; Jun 2 '20, 10:23 AM. Reason: added code tags
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    lvalue needs to be present on the left-hand side of the = operator.

    If the aim is to assign value to a pointer, it needs to be initialized first.

    Edit: Explain what you're trying to achieve and always provide sufficient code so that the other party can reproduce the same error.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Lines 19-26 and 34 have the increment (++) operator. Which do you intend to increment -- pointer p or the thing pointed at by p? Will the tricky looking parentheses and cast achieve your intention?
      Notice that argument p is a void*. That means the compiler doesn't know the type of the thing pointed at by p, so it doesn't know the size of the thing pointed at by p, so it doesn't know how much to increment the pointer by. The code tries to get around that by casting p to a UDINT* and then a USINT*. The errors suggest the compiler is not satisfied by this tactic.

      Try getting rid of the casts:
      Code:
      ...
          UDINT *udp = (UDINT*)p;
          *udp++ = longValue;
          ...
          *udp++ = longValue;
          p = (void*)udp;
          ...
          USINT *usp = (USINT*)p;
          *usp++ = (USINT)longValue;
          p = (void*)usp;
      I show casts for the udp, usp, and p assignments. C does not require them but I think C++ does. If you don't need them then leave them out.

      By the way, line 9 casts a UDINT value into a USINT. Make sure no information is lost or corrupted by that cast. Every cast is a red flag that you're doing something tricky and risky.

      By the way, line 15 should probably be casting p to a size_t.

      By the way, the register keyword on line 11 probably doesn't do anything useful. I would remove it.

      Comment

      Working...