0

I'm trying to make a function that takes a void*, copies some memory to it, and then moves the pointer. Since it is a void pointer, I thought I'd cast it to char* and move that, like so:

PVOID SendAndMoveHead(PVOID dest, const Message& message, const size_t& size)
{
    PVOID ret = CopyMemory(dest, (PVOID)message.msg.c_str(), size);
    ((char*)dest) += size;
    return ret;
}

However, VS complains about ((char*)dest) saying

expression must me a modifiable lvalue

which I thought it was, since the following works:

PVOID SendAndMoveHead(PVOID dest, const Message& message, const size_t& size)
{
    PVOID ret = CopyMemory(dest, (PVOID)message.msg.c_str(), size);
    char* d = (char*)dest;
    d += size;
    return (PVOID)d;
}

If someone could shed some light on why the first version shouldnt work I'd really appreciate it.

Elias Finoli
  • 121
  • 9

1 Answers1

2

((char*)dest) gives you a new temporary char *. ((char*)dest) += size; would change the temporary and have no effect, which causes a compilation failure.

In the second example d is not a temporary and lives long enough to get returned.

Alternatively you could write return (char*)dest + size;.

nwp
  • 9,623
  • 5
  • 38
  • 68