Byte Alignment of Character to 2 byte

V

v.venkatesh

Hi,

For one of our design, we had introduced a local array of structures
which contains the following fields :

typedef struct {
W_CHAR ussdService[MAX_CHARACTER];
W_CHAR ussdCommand[MAX_NUMBER];
BOOL isTrue;
} NSussdStruct;


The above structure is used to store a Ussd string.

const NSussdStruct UssdString_To_Operation_tbl[]=
{
{ L"*126#" , ACTIVATE },
{ L"#126#" , DEACTIVATE },
{ L"*#126#" , STATUS_REQUEST },

}

We have used the keyword "L" to specify the characters to be word
aligned in the structure array, so that its compatible with the lower
layer calls. The above implementation has been verified to be working
fine on compiler.

Our question is, this feature a language dependent or compiler
dependent feature. Any reference to ANSI C would be useful.
Also, if any one has implemented the 2 byte CHAR assigment statement
with the keyword "L", can you please let me know if the implementation
is safe. Also, please suggest if you have a better strategy.
 
L

Lawrence Kirby

Hi,

For one of our design, we had introduced a local array of structures
which contains the following fields :

typedef struct {
W_CHAR ussdService[MAX_CHARACTER];
W_CHAR ussdCommand[MAX_NUMBER];
BOOL isTrue;
} NSussdStruct;


The above structure is used to store a Ussd string.

const NSussdStruct UssdString_To_Operation_tbl[]=
{
{ L"*126#" , ACTIVATE },
{ L"#126#" , DEACTIVATE },
{ L"*#126#" , STATUS_REQUEST },

}

We have used the keyword "L" to specify the characters to be word
aligned in the structure array, so that its compatible with the lower
layer calls. The above implementation has been verified to be working
fine on compiler.

The sequence L" intoduces a wide character string literal which is a
standard construct and has elements of type wchar_t.
Our question is, this feature a language dependent or compiler dependent
feature. Any reference to ANSI C would be useful. Also, if any one has
implemented the 2 byte CHAR assigment statement with the keyword "L",
can you please let me know if the implementation is safe. Also, please
suggest if you have a better strategy.

While standard wchar_t isn't guaranteed to be 2 bytes or aligned in any
particular way. Like any other integer type the standard doesn't specify
representation details such as byte order.

Whether it is appropriate depends on how the "lower layer calls" are
specified. IF it is in terms of wchar_t this is fine, if it is in terms of
a character array then this isn't portable. If there are specific
alignment requirements beyong those guaranteed by the type you may have to
use something like malloc() to guarantee alignment portably.

OTOH it is possible that the "lower layer calls" are specific to
implementations which provide greater guarantees than standard C and your
code may be OK.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top