0

Compiling simple c program on arm platform.

typedef struct
{
    char* Case;
    int  RespCmdLen;
    unsigned char *RespCommand;

}ResponseStruct;
int main()
{
    unsigned char CommandResp[] = { 
        0x01,
        0x08, 0x07,
        0x05, 0x00,
        0x00,
        0x00,
        0x0B,
        0x00,
        0x00,
    };
    ResponseStruct CaseRespTbl[] =
    {
         /* case,           Response length,                response buffer pointer   */
         { "case1",             sizeof(CommandResp),        CommandResp},
    };

    return 0;
}

and got error like

Error:  #24: expression must have a constant value
         { "case1",             sizeof(CommandResp),        CommandResp},
                                                            ^

But if i change that code to

ResponseStruct CaseRespTbl[10];
CaseRespTbl[0].Case = "case1";
CaseRespTbl[0].RespCmdLen = sizeof(CommandResp);
CaseRespTbl[0].RespCommand = CommandResp;

Then it will compiled without any issue.

Any reason for that?

6
  • 4
    Non-constant initialisers are a C99 thing - you haven't said which compilers you're using and how you're compiling on each, but I suspect once you have that information you'll also have the answer ;) Commented Feb 27, 2015 at 12:09
  • I am using ARM RVCT 3.1 toochain Commented Feb 27, 2015 at 12:13
  • 1
    RVCT assumes C90 unless you tell it otherwise - I guess you're not telling it otherwise? Commented Feb 27, 2015 at 12:17
  • You could move the variables to global scope; they look pretty global + constant to me. Commented Feb 27, 2015 at 12:31
  • @joop that wouldn't help either. also, why globals? like, at all? Commented Feb 27, 2015 at 14:18

1 Answer 1

1

Assuming the command response is constant, I would simply ditch it and use:

ResponseStruct CaseRespTbl[] = {
    #  Case    Sz   Bytes
    { "case1", 10, "\x01\x08\x07\x05\x00\x00\x00\x0b\x00\x00" },
};

You can still use sizeof if you want, by putting that final string into a #define, something like:

#define CMD_RESP "\x01\x08\x07\x05\x00\x00\x00\x0b\x00\x00"
ResponseStruct CaseRespTbl[] = {
    #  Case    Sz                  Bytes
    { "case1", sizeof(CMD_RESP)-1, CMD_RESP },
};

but that's probably not necessary for data that doesn't change very often.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.