lvalue required as increment operand

Discussion in 'C Programming' started by Ben Pfaff, Dec 24, 2007.

  1. Ben Pfaff

    Ben Pfaff Guest

    "Roman Mashak" <> writes:

    > extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
    > {
    > unsigned int dI;
    >
    > for (dI = 0; dI < dLength; dI++) {
    >
    > *((char *) pDest) = *((char *) pSource);
    > ((char *) pDest)++; /* error */
    > ((char *) pSource)++; /* error */
    > }
    > }


    extern inline void copy(void *pSource_, void *pDest_, unsigned int dLength)
    {
    char *pSource = pSource_;
    char *pDest = pDest_;
    unsigned int dI;

    for (dI = 0; dI < dLength; dI++) {
    *pDest = *pSource;
    pDest++;
    pSource++; /* error */
    }
    }

    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Dec 24, 2007
    #1
    1. Advertising

  2. Ben Pfaff

    Eric Sosman Guest

    Roman Mashak wrote:
    > Hello,
    >
    > I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
    > This snippet results in error, posted in subject:
    >
    > /*
    > * copies a structure in a buffer
    > * pSource Data to copy
    > * pDest Buffer in which to copy the data
    > */
    > extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
    > {
    > unsigned int dI;
    >
    > for (dI = 0; dI < dLength; dI++) {
    >
    > *((char *) pDest) = *((char *) pSource);
    > ((char *) pDest)++; /* error */
    > ((char *) pSource)++; /* error */
    > }
    > }
    >
    > As I understand, casting ends up with 'rvalue', and '++' or any other
    > operations can't be applied to rvalue. So, what is the proper solution here?
    > I'm unable to find a way :(


    Ben Pfaff has shown how to fix the code, but I'll offer
    another solution:

    #include <string.h>
    extern inline void copy(void *pSource, void *pDest,
    unsigned int dLength)
    {
    memcpy (pDest, pSource, dLength); /* note arg swap */
    }

    If you're using a free-standing rather than a hosted
    implementation, it's possible that <string.h> and memcpy()
    are not provided, but take a look anyhow: If they're present,
    they'll likely be speedier than anything written in plain C.
    Also, memcpy() will behave unpredictably if the source and
    destination areas overlap; you could use memmove() to get
    predictable behavior, but that behavior might not be precisely
    what you get from the original. You need to study the "contract"
    of the copy() function to see if it's a candidate for replacement.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 24, 2007
    #2
    1. Advertising

  3. "Roman Mashak" <> writes:
    > I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
    > This snippet results in error, posted in subject:
    >
    > /*
    > * copies a structure in a buffer
    > * pSource Data to copy
    > * pDest Buffer in which to copy the data
    > */
    > extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
    > {
    > unsigned int dI;
    >
    > for (dI = 0; dI < dLength; dI++) {
    >
    > *((char *) pDest) = *((char *) pSource);
    > ((char *) pDest)++; /* error */
    > ((char *) pSource)++; /* error */
    > }
    > }
    >
    > As I understand, casting ends up with 'rvalue', and '++' or any other
    > operations can't be applied to rvalue. So, what is the proper solution here?

    [...]

    Another possibility is to change the two "++"s to:

    pDest = (char*)pDest + 1;
    pSource = (char*)pSource + 1;

    The void* values are explicitly converted to char* so you can
    increment them, then the char* result is implicitly converted back to
    void* by the assignment.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 24, 2007
    #3
  4. Ben Pfaff

    Flash Gordon Guest

    Eric Sosman wrote, On 24/12/07 05:17:
    > Roman Mashak wrote:
    >> Hello,
    >>
    >> I'm porting code, previously compiled by IAR compiler, for ARM-gcc
    >> compiler. This snippet results in error, posted in subject:
    >>
    >> /*
    >> * copies a structure in a buffer
    >> * pSource Data to copy
    >> * pDest Buffer in which to copy the data
    >> */
    >> extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
    >> {
    >> unsigned int dI;
    >>
    >> for (dI = 0; dI < dLength; dI++) {
    >>
    >> *((char *) pDest) = *((char *) pSource);
    >> ((char *) pDest)++; /* error */
    >> ((char *) pSource)++; /* error */
    >> }
    >> }
    >>
    >> As I understand, casting ends up with 'rvalue', and '++' or any other
    >> operations can't be applied to rvalue. So, what is the proper solution
    >> here? I'm unable to find a way :(

    >
    > Ben Pfaff has shown how to fix the code, but I'll offer
    > another solution:
    >
    > #include <string.h>
    > extern inline void copy(void *pSource, void *pDest,
    > unsigned int dLength)
    > {
    > memcpy (pDest, pSource, dLength); /* note arg swap */
    > }


    Unless you explicitly need the function version I would use a macro instead.

    > If you're using a free-standing rather than a hosted
    > implementation, it's possible that <string.h> and memcpy()
    > are not provided, but take a look anyhow: If they're present,


    At least some free-standing implementations provide memcpy (and as much
    of the rest of the standard library as they sensibly can).

    > they'll likely be speedier than anything written in plain C.
    > Also, memcpy() will behave unpredictably if the source and
    > destination areas overlap; you could use memmove() to get
    > predictable behavior, but that behavior might not be precisely
    > what you get from the original. You need to study the "contract"
    > of the copy() function to see if it's a candidate for replacement.


    Indeed. Personally I would want to change the code that calls copy to
    call memmove or memcpy as appropriate. Then there is one less function
    for the next maintainer to learn.
    --
    Flash Gordon
     
    Flash Gordon, Dec 24, 2007
    #4
  5. Ben Pfaff

    Roman Mashak Guest

    Hello,

    I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
    This snippet results in error, posted in subject:

    /*
    * copies a structure in a buffer
    * pSource Data to copy
    * pDest Buffer in which to copy the data
    */
    extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
    {
    unsigned int dI;

    for (dI = 0; dI < dLength; dI++) {

    *((char *) pDest) = *((char *) pSource);
    ((char *) pDest)++; /* error */
    ((char *) pSource)++; /* error */
    }
    }

    As I understand, casting ends up with 'rvalue', and '++' or any other
    operations can't be applied to rvalue. So, what is the proper solution here?
    I'm unable to find a way :(

    Thanks.

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Dec 24, 2007
    #5
  6. Ben Pfaff

    Ahmed Samieh Guest

    On Dec 24, 11:24 pm, "Roman Mashak" <> wrote:
    > Hello,
    >
    > I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
    > This snippet results in error, posted in subject:
    >
    > /*
    > * copies a structure in a buffer
    > * pSource Data to copy
    > * pDest   Buffer in which to copy the data
    > */
    > extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
    > {
    >     unsigned int dI;
    >
    >     for (dI = 0; dI < dLength; dI++) {
    >
    >         *((char *) pDest) = *((char *) pSource);
    >         ((char *) pDest)++;    /* error */
    >         ((char *) pSource)++;    /* error */
    >     }
    >
    > }
    >
    > As I understand, casting ends up with 'rvalue', and '++' or any other
    > operations can't be applied to rvalue. So, what is the proper solution here?
    > I'm unable to find a way :(
    >
    > Thanks.
    >
    > With best regards, Roman Mashak.  E-mail:


    void* mcpy(void *dst, void *src, size_t n)
    {
    size_t i;
    char* ldst = (char*)dst;
    char* lsrc = (char*)src;
    for (i = 0; i < n; ++i)
    {
    *ldst++ = *lsrc++;
    }
    return dst;
    }
     
    Ahmed Samieh, Dec 24, 2007
    #6
  7. Ben Pfaff

    Roman Mashak Guest

    Hello, Eric!
    You wrote on Mon, 24 Dec 2007 00:17:21 -0500:

    [skip]
    ES> Ben Pfaff has shown how to fix the code, but I'll offer
    ES> another solution:

    ES> #include <string.h>
    ES> extern inline void copy(void *pSource, void *pDest,
    ES> unsigned int dLength)
    ES> {
    ES> memcpy (pDest, pSource, dLength); /* note arg swap */
    ES> }
    [skip]

    Thanks a lot!

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Dec 24, 2007
    #7
  8. Ben Pfaff

    CBFalconer Guest

    Roman Mashak wrote:
    >
    > I'm porting code, previously compiled by IAR compiler, for ARM-gcc
    > compiler. This snippet results in error, posted in subject:
    >
    > /*
    > * copies a structure in a buffer
    > * pSource Data to copy
    > * pDest Buffer in which to copy the data
    > */
    > extern inline void copy(void *pSource, void *pDest, unsigned int dLength) {
    > unsigned int dI;
    >
    > for (dI = 0; dI < dLength; dI++) {
    > *((char *) pDest) = *((char *) pSource);
    > ((char *) pDest)++; /* error */
    > ((char *) pSource)++; /* error */
    > }
    > }
    >
    > As I understand, casting ends up with 'rvalue', and '++' or any
    > other operations can't be applied to rvalue. So, what is the
    > proper solution here? I'm unable to find a way :(


    /* copies a structure in a buffer. */
    extern inline void copy(void *psor, void *pdst, size_t lgh) {
    char *cs = psor, cd = pdst;

    while (lgh--) *cd++ = *cs++;
    } /* untested */

    Notice the absence of casts, which are usually errors. Also note
    the change in the type of the lgh parameter. The above should drop
    in wherever you were calling the old version. Any reasonable
    compiler will absorb the extra data items.

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee, Frohe Weihnachten
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 25, 2007
    #8
  9. Ben Pfaff

    Joe Wright Guest

    Roman Mashak wrote:
    > Hello,
    >
    > I'm porting code, previously compiled by IAR compiler, for ARM-gcc compiler.
    > This snippet results in error, posted in subject:
    >
    > /*
    > * copies a structure in a buffer
    > * pSource Data to copy
    > * pDest Buffer in which to copy the data
    > */
    > extern inline void copy(void *pSource, void *pDest, unsigned int dLength)
    > {
    > unsigned int dI;
    >
    > for (dI = 0; dI < dLength; dI++) {
    >
    > *((char *) pDest) = *((char *) pSource);
    > ((char *) pDest)++; /* error */
    > ((char *) pSource)++; /* error */
    > }
    > }
    >
    > As I understand, casting ends up with 'rvalue', and '++' or any other
    > operations can't be applied to rvalue. So, what is the proper solution here?
    > I'm unable to find a way :(
    >
    > Thanks.
    >
    > With best regards, Roman Mashak. E-mail:
    >
    >

    Too complicated. First, if copy() would use pSourse and pDest as char*
    it should declare them such. :

    void copy(char *pSource, char *pDest, unsigned dLength)

    Even if you call it with void* the compiler will do the conversions to
    char* for you.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Dec 25, 2007
    #9
  10. Joe Wright <> writes:
    > Roman Mashak wrote:
    >> I'm porting code, previously compiled by IAR compiler, for ARM-gcc
    >> compiler. This snippet results in error, posted in subject:
    >>
    >> /*
    >> * copies a structure in a buffer
    >> * pSource Data to copy
    >> * pDest Buffer in which to copy the data
    >> */
    >> extern inline void copy(void *pSource, void *pDest, unsigned int dLength)

    [...]
    > Too complicated. First, if copy() would use pSourse and pDest as char*
    > it should declare them such. :
    >
    > void copy(char *pSource, char *pDest, unsigned dLength)
    >
    > Even if you call it with void* the compiler will do the conversions to
    > char* for you.


    I disagree. This kind of thing is exactly what void* is for (see the
    standard memcpy() and memmove() functions, for example). If the
    parameters are declared as char* (why not unsigned char*?), the
    function can't be used with, say, int* arguments without a cast. Any
    required explicit conversions should be done inside the function, not
    imposed on the user.

    Of course, just using memcpy() or memmove() directly is probably
    better than re-implementing it.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 25, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kavya
    Replies:
    9
    Views:
    515
    Dik T. Winter
    Oct 28, 2006
  2. Replies:
    11
    Views:
    701
    James Kuyper
    Sep 22, 2008
  3. Pavel
    Replies:
    13
    Views:
    1,093
    Pavel
    Jul 5, 2011
  4. Leo \Equinox\ Gaspard
    Replies:
    1
    Views:
    538
    Alf P. Steinbach /Usenet
    Jul 3, 2011
  5. Leo \Equinox\ Gaspard
    Replies:
    2
    Views:
    396
    James Lothian
    Jul 3, 2011
Loading...

Share This Page