C++ expression is not assignable when writing `&str_send[0] = &str_recv[c * 6];`

I'm trying to parse an array that receives a multiple of 8 bytes and sends back each array of 8 bytes one at a time.

I'm getting an expression is not assignable or lvalue required as left operand of assignment error when building

I'm trying to figure out why I cannot simply change the address of an array to the new position. At first I thought it was a C-style array issue, but the same error happened when I tried with std::vector<unsigned char>

Is there a preferable way of doing this without copying the bytes?

Thanks,

unsigned char str_send[8];
unsigned char str_recv[BUF_SIZE];

int n = receive(cport_nr, str_recv, (int)BUF_SIZE);

if (n > 0 && ( n % 8 == 0 ) )
{
    for (int c = 0; c < n / 8; c++) //Break up multiple 8-byte chunks
    {       
        &str_send[0] = &str_recv[c * 6]; //ERROR expression is not assignable
    }
}
return (0);
728x90

1 Answers C++ expression is not assignable when writing `&str_send[0] = &str_recv[c * 6];`

You can't change the address of a variable.

What you could do is create an array of pointers to the original array, but then you're just copying addresses (probably 8 bytes each) instead of single byte values. Then you've have to dereference those pointers, which makes it non-trivial to send what they point to.

Copying the bytes is exactly what you want to do here. Then you have a buffer you can send as-is:

str_send[0] = str_recv[c * 6];

4 months ago