Reputation: 1447
I am very beginner in C. I have the following structure
typedef struct
{
zuint8 u8ZCLVersion;
#ifdef CLD_BAS_ATTR_LOCATION_DESCRIPTION
tsZCL_CharacterString sLocationDescription;
uint8 au8LocationDescription[16];
#endif
#ifdef CLD_BAS_ATTR_PHYSICAL_ENVIRONMENT
zenum8 u8PhysicalEnvironment;
#endif
#ifdef CLD_BAS_ATTR_DEVICE_ENABLED
zbool bDeviceEnabled;
#endif
} tsCLD_Basic;
now i want to set au8LocationDescription[16] field. And I am using this piece of code.
tsCLD_Basic sCombined;
sCombined.au8LocationDescription[16] = {0x42,0x65,0x64,0x20,0x52,0x6f,0x6f,0x6d};
but its shows error error: expected expression before '{' token
how could I write the values..???
Upvotes: 0
Views: 134
Reputation:
sCombined.au8LocationDescription[16] = {0x42,0x65,0x64,0x20,0x52,0x6f,0x6f,0x6d};
What this line tells the compiler to do is to assign {0x42,0x65,0x64,0x20,0x52,0x6f,0x6f,0x6d}
to the sixteenth element of array au8LocationDescription.
It won't work. First, au8LocationDescription[16] is not a valid location. Writing anything there results in undefined behavior. Your array has only 16 elements, so you are only allowed to used indices from 0 to 15. And it doesn't even represent an array, its an int.
But since you're trying to fill up the array with some values, that is irrelevant. You might try
sCombined.au8LocationDescription = {0x42,0x65,0x64,0x20,0x52,0x6f,0x6f,0x6d};
but that won't work either. You can't assign to arrays that way. This trick is only allowed in initialization.
What remains is assigning the elements of the array one-by-one. But if you want to save LOC's, you can do something along these lines:
static uint8 values[] = {0x42,0x65,0x64,0x20,0x52,0x6f,0x6f,0x6d};
memcpy(sCombined.au8LocationDescription, values, sizeof(values));
Upvotes: 1
Reputation: 21351
As the comment of Als says, what you are trying to do is not possible. You will need to assign each array element separately like this
sCombined.au8LocationDescription[0] = 0x42;
sCombined.au8LocationDescription[1] = 0x65;
...
and so on until each element is has the value you want.
Upvotes: 1