[fpc-pascal] struct definition in C

Bernd Oppolzer bernd.oppolzer at t-online.de
Sun Mar 23 20:06:28 CET 2014


Am 23.03.2014 19:54, schrieb Jonas Maebe:
> On 23/03/14 19:39, Darius Blaszyk wrote:
>
>> What would be the equivalent of the following struct definition in C?
>>
>> typedef struct MyStruct
>> {
>>   unsigned a :6;
>>   unsigned b :15;
>>   unsigned c:11;
>> }MyStruct;
>>
>> I imagine this is some sort of struct initialization. Is thi spossible
>> in FPC as well, or what is the best alternative?
>
> It's not an initialization, it's a struct with bitfields. FPC 
> currently does not support C-compatible bitfield packing. To get a 
> record with equal alignment and size you can declare it as "record 
> data: cuint; end;", but there is currently no way in FPC to access the 
> bitfields in a portable way across platforms.
>
>
> Jonas
>
I would like to add that bitfields in C are not at all portable;
it is completely implementation dependant if they are
allocated from right to left in the structure or the other way round etc.;

I do lot of portable development in C, and I never use bitfields for 
that reason;
if I really need to store information in single bits, I use ints or chars
and access the individual bits using logical operations. Of course,
the problem with big-endian / little-endian has to be taken into 
account, too
(not, if you use unsigned chars as the base for the bit strings, and if 
the chars are
eight bits long, which should be the case on all modern architectures).

Kind regards

Bernd




More information about the fpc-pascal mailing list