c - assignment of packed bitfield struct gives wrong values -


i have packed structure should 16 bits:

#pragma pack(1)  typedef unsigned int uint; typedef unsigned short ushort;  typedef struct color color; struct color {     uint r : 5;     uint g : 5;     uint b : 5;     uint _ : 1; }; 

i have confirmed that

&(((color*)0x06000000)[x]) == &(((ushort*)0x06000000)[x]) 

for various values of x. however, in code, these 2 lines give me different results.

void write_pixel (uint x, uint y, color color) {     #if 0     ((color*)0x06000000)[x+y*240] = color;     #else      ((ushort*)0x06000000)[x+y*240] = ((ushort*)&color)[0];     #endif } 

with second option being correct.

what cause?

note. i'm compiling gba emulator no debugger or printf, yes/no statements way of green/red pixel.

the problem value seen parameter list going (due promotion of parameters) unsigned int (32bits). incorrect line trying copy 32bit unsigned int 16bit short.

normally work pragma modifies how 16bit color being placed in promoted parameter and/or how code referencing 16bits of color.

while packed values (normally) not promoted, if there no prototype called function parameters assumed 'int'  code assumes data (sizeof int) bytes long (4 bytes) , assignment takes last 2 bytes of 4 byte value rather first 2 bytes. 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -