Search code examples
c++lvalue

Expression must be a modifiable lvalue (when attempting to append to an array)


I have the following:

uint16_t Hitcount[64]; //64-byte array buffer 
uint16_t Br; 
int StdWidth = 38;
int widthpad = 8;
int W;
uint16_t blocks;


if (W <= (StdWidth + widthpad) && W >= (StdWidth - widthpad) && blocks == 1) {
        Hitcount += Br;
    }

My goal is to appenf "Br" to the array "Hitcount" if "W" is within a certain range. However, "Hitcount" is indicating the error "Expression must be a modifiable lvalue". Don't I have my data types and everything in order?

Apologies if this is too close to other questions that have been posted. I looked at them but could not relate them to this scenario with my limited knowledge.


Solution

  • Hitcount += Br;
    

    You cannot add value to a C style array like that. You either need to maintain elements count and add a value like this:

    Hitcount[count++] = Br;
    

    or you better use std::vector and add element by calling push_back:

    std::vector<uint16_t> Hitcount;
    // code skipped
    Hitcount.push_back( Br );