简体   繁体   中英

Error in c program when compiling on arm platform

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM