On Sat, 7 Feb 2004 10:48:46 -0500, "Regis St-Gelais"
<[EMAIL PROTECTED]> wrote:
>Here is a peace of code:
>
>typedef struct tagRGBTRIPLE {
> UInt8 Red;
> UInt8 Green;
> UInt8 Blue;
>} RGBTRIPLE;
>
>UInt8 gustrArray[256];
>
>void foo(void)
> {
> Int16 intI;
> RGBTRIPLE *rgbpTriplet;
> UInt8 uchrR,uchrG,uchrB;
> for (intI=0;intI<256;intI++)
> {
> gustrArray[intI]=intI;
> }
> rgbpTriplet=(RGBTRIPLE *)gustrArray;
> uchrR=rgbpTriplet[10].Red;
> uchrG=rgbpTriplet[10].Green;
> uchrB=rgbpTriplet[10].Blue;
> }
>
>
>I would expext to get uchrR=30, uchrG=31 and uchrB=32
>But I get uchrR=40, uchrG=41 and uchrB=42 as if the struct had 4 bytes.
This is an illustration of the danger of using casts. Whenever you
use a cast, you are turning off the usual compiler protections that
make struct alignment and padding transparent. When you force the
compiler to treat gustrArray as if it were a pointer to RGBTRIPLE, you
are taking full responsibility for the consequences of that
assumption. You are right that the compiler is assuming that the
struct takes up four bytes when combined in arrays of structs, but
that is not a bug.
Robert Scott, Ypsilanti, MI
(reply through this forum, not by e-mailing me directly)
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/