Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


How to initialise array RESOURCE members

<array-initialiser>
array-initialiser ::=
{ <array-initialiser-item-comma-list> }
<array-initialiser-item>
array-initialiser-item ::=
<initialiser>

To initialise a member of array type, give the items in the array in sequence. Each initialiser must be of a type compatible with the member being initialised.

Fixed-length arrays

If a member is declared as a fixed-length array in the STRUCT or RESOURCE definition, for example,

WORD elements[10]

then you must not specify any more items than were given in the length.

If fewer items are specified in the default initialisation (i.e. in the STRUCT definition), then an error also results.

Note that no values will be given to unspecified elements at the end of the array, even if they have been default initialised in the STRUCT definition. Take the following example:

STRUCT SAMPLE
 {
 BYTE bts[3]={1,2,3};
 }

In the following resource:

RESOURCE SAMPLE default
 {}

the output will be the whole default array

0x01 0x02 0x03

but in this resource:

RESOURCE SAMPLE first_specified
 {
 bts={5};
 }

the output is:

0x05

with the second and third elements lost.

If you specify only the second element in the RESOURCE definition, then the first element is taken from the default initialisation, the second from the explicit initialisation and the third element is lost. The following resource:

RESOURCE SAMPLE  second_specified
 {
 bts[1]=5;
 }

results in the 2-byte output:

0x01 0x05

If, however, you explicitly initialise an element in the middle of an array without having supplied default values for array members before it, then an error will result.

[Top]


Using expressions to initialise array elements

You may initialise array elements with expressions. You must explicitly initialise each member component of the array otherwise the expressions will be evaluated incorrectly. The following resource:

RESOURCE SAMPLE correct_expression
 {
 bts[0]=3+1;
 bts[1]=2;
 bts[2]=3;
 }

will generate the correct output 0x04 0x02 0x03. However, if you use the following syntax:

RESOURCE SAMPLE incorrect_expression
 {
 bts={3+1,2,3};
 }

the output will be 0x03 0x02 0x03. This is because the pre-processor treats 3+1 as a literal string that is then interpreted by the compiler as 3. In the resource correct_expression above the ‘=‘ sign forces the pre-processor to evaluate the expression.