Keil™, An ARM® Company

Technical Support

CARM: ALIGNMENT FOR CHAR AND SHORT


Information in this article applies to:

  • CARM All Versions

QUESTION

I have an array of byte (unsigned char) values. Since my source code uses some tricks to access this array, I want to force the char variable to a 32-bit (int) boundary. Is there a way to change the alignment with a C pragma directive?

ANSWER

The CARM User's Guide contains a section that discusses how to align variables to various boundaries. While it is possible to change the default alignment for int or short to a byte-alignment, it is not possible to force char variables to a 32-bit alignment.

However, you may define the char array in a union that contains an int variable as shown below:

union u {
  unsigned char c[10];  // the char array
  unsigned int  i;      // force 32-bit int alignment
};

  union u array;
  :
  array.c[i] = 0;       // access to int-align char array
  :

MORE INFORMATION

Last Reviewed: Friday, July 15, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure