Search code examples
c++pointersvoid-pointerslvalue

"lvalue required as left operand of assignment" in odd place--C++


NOTE: This is NOT a duplicate of the billion and one questions with this name in the title. This has to do with pointers and very odd stuff, not an accidental = instead of ==.

I have a C++ function in which I have a void* argument called out. I have this line:

(char*)out=new char[*size];

Where size in a uint32_t*. The compiler complains:

fundemental_bin_types.h:55:32: error: lvalue required as left operand of assignment

What is wrong?


Solution

  • (char*)out is not an lvalue (out itself may be but the cast changes things), the cast belongs on the other side of the assignment and you're casting to a void pointer rather than a char pointer:

    out = (void*) (new char[*size]);
    

    In any case, void* is implicitly convertible from other pointers so you can get away with just:

    out = new char[*size];