Reputation: 25875
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?
Upvotes: 0
Views: 76
Reputation: 881403
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.
Upvotes: 1