Keil Logo

C166: PACK DIRECTIVE


Information in this article applies to:

  • C166 Version 4.06

QUESTION

What does the PACK directive do? Does it let me pack structure elements on byte or word boundaries?

ANSWER

Yes. That's exactly what the PACK directive does.

The PACK directive causes the C166 Compiler to generate byte-aligned structures with word elements. This is useful when exchanging data structures with other systems where no alignment is required.

NOTE: The C166 compiler generates considerably more code to access byte-aligned words. For this reason, PACK(1) should be used only when necessary.

PACK(1) assumes that structure pointers point to word-aligned structures. If your application uses structure pointers to byte-aligned structures, you must use the BYTEALIGN directive in addition to PACK(1).

For example:

#pragma pack(1)   /* byte alignment */

struct s1  {
  int  i1;     // i1 has offset 0
  char c1;     // c1 has offset 2
  int  i2;     // i2 has offset 3
  char c2;     // c2 has offset 5
  int  i3;     // i3 has offset 6
  char z1;     // z1 has offset 8
};

#pragma pack()    /* reset to default alignment */

struct s2  {
  int  i1;     // i1 has offset 0
  char c1;     // c1 has offset 2
  int  i2;     // i2 has offset 4
  char c2;     // c2 has offset 6
  int  i3;     // i3 has offset 8
  char z1;     // z1 has offset 10
};

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Thursday, September 7, 2000


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.