This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Arrays in C



Hi I have a very silly question. I have been getting this error and I can't figure out why

char value = 0x08;

char Command[2] = {value, 0x00};

I get an error: expression must have a constant value, if I try to compile this way, but i don't get an error if I try to compile it this way

char Command[2] = {0x08, 0x00};

i don't see why it will throw me the error

Thanks

  • I think it's pretty clear. You variable value, is not a fixed value, it may change, so it doesn't know how to initialize the variable Command.

    I see two possible solutions here, the first one, make value a fixed value:

    #define VALUE      0x08
    char Command[2] = {VALUE, 0x00};

    The other one, initialize it later:

    char value = 0x08;
    char Command[2];
    Command[0] = value;
    Command[0] = 0x00;
    

    I have not tried these, but I guess they will work.

    Good luck.

  • If you're willing to compile the file as a C++ file, this works:
    const char value = 0x08;
    char Command[2] = {value, 0x00};