0

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..???

1
  • 1
    You are trying to initialize the array member after is has been created.You can only initialize the array in statement after that You can only assign individual elements of the array, because arrays are not assignable. Commented Jun 20, 2012 at 6:48

2 Answers 2

1

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.

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

Comments

1
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));

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.