Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving C++ 'target of assignment not really an lvalue' errors

Given this code:

void FrMemCopy(void *to, const void *from, size_t sz)
{
    size_t sz8 = sz >> 3;
    size_t sz1 = sz - (sz8 << 3);

    while (sz8-- != 0) {
        *((double *)to)++ = *((double *)from)++;
    }

    while (sz1-- != 0) {
        *((char *)to)++ = *((char *)from)++;
    }
}

I am receiving target of assignment not really an lvalue warnings on the 2 lines inside the while loops.

Can anyone break down those lines?

a cast then an increment?

What is a simplier way to write that?

What does the error mean?

like image 320
Jasmine Avatar asked Feb 19 '23 15:02

Jasmine


2 Answers

It does not like the *((char*)to)++ statement.

Try this:

void FrMemCopy(void *to, const void *from, size_t sz)
{
    size_t sz8 = sz >> 3;
    size_t sz1 = sz - (sz8 << 3);
    double * tod = (double *) to;
    double * fromd = (double *) from;
    while (sz8-- != 0) {
        *(tod++) = *(fromd++);
    }

    char * toc = (char *) tod;
    char * fromc = (char *) fromd;
    while (sz1-- != 0) {
        *(toc++) = *(fromc++);
    }
}
like image 87
therealsachin Avatar answered Feb 25 '23 02:02

therealsachin


You can't apply ++ to the result of a cast, only to an lvalue (a variable). So you need to create new variable with the appropriate types for the increments:

void FrMemCopy(void *to, const void *from, size_t sz)
{
    size_t sz8 = sz >> 3;
    size_t sz1 = sz - (sz8 << 3);

    double *to1 = (double *)to;
    double *from1 = (double *)from
    while (sz8-- != 0) {
        *to1++ = *from1++;
    }

    char *to2 = (char *)to1;
    char *from2 = (char *)from1;
    while (sz1-- != 0) {
        *to2++ = *from2++;
    }
}
like image 38
Chris Dodd Avatar answered Feb 25 '23 04:02

Chris Dodd