Hi,
I'm working with at91sam7s256. Of course i'm using Keil. I want to align struct with bitfields one by one because a want to cast buffer on my struct and read data using that struct. the problem is that:
i made struct with attribute packed and then size of this struct is 18 bytes, without attribute packed size struct is 20 bytes, but struct should have got 16 bytes.
Can anyone help me? I dont know how to force Keil to compile this struct in one piece without unused fields betweed struct fields.
the structure should have 16 bytes
No, it shouldn't. You want it to have that size, but there's no particularly compelling reason why this wish should be granted. Bitfields are meant for internal work of a C program, not for duplicating external storage patterns.
The lesson to be learned is: Never make any assumptions about the storage representation of bitfields. Each individual bitfield will hold values within its defined range and signedness. All other aspects of bitfields are outside your control, and you should never rely on them. If you really need to control the storage layout of objects of fractional size (in bytes), you have to use shifting and masking on arrays of unsigned char.
This issue is one of the typical mistakes just about every C programmer makes at some point on their learning curve. The only way they could be avoided would be good textbooks, skilled teachers --- both of which are dangerously rare.