[fpc-pascal] Porting C macro

Darius Blaszyk dhkblaszyk at zeelandnet.nl
Tue Mar 12 02:48:31 CET 2013


 

Henry Vermaak schreef op 12 mrt '13: 

> On Mon, Mar 11, 2013 at
11:26:57PM +0100, Darius Blaszyk wrote:
> 
>> I'm stuck porting a macro
from C. Below is the original define. The part I'm struggeling with is
the right most part after the - sign. #define GETNEXT(x) ((LList
*)(((char *) x) - ((char *) & (((LList *)0)^.next))))
> 
> I'm assuming
that the "^." should be "->", in which case the last bit is
> doing the
same as the offsetof() macro. So the macro is subtracting the
> offset
in bytes of the "next" member of the LList struct from x, then
> casting
it to (LList *).
> 
> I'd wager that the GETNEXT() isn't actually doing
what it says, but
> something more like container_of() (because it's
_subtracting_ the
> offset). I may be wrong, though, do you have an
example of its usage?
> 
> Henry
>
_______________________________________________
> fpc-pascal maillist -
fpc-pascal at lists.freepascal.org
>
http://lists.freepascal.org/mailman/listinfo/fpc-pascal [1]

I will let
your email sink in later, but here is a usage example as requested

 if
(list->next) GETNEXT(list->prev)->nextname = GETNEXT(list->next)->name;

else GETNEXT(list->prev)->nextname = 0;

Both name and nextname are
pchar's.

Regards, Darius 

 

Links:
------
[1]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130312/1934842e/attachment.html>


More information about the fpc-pascal mailing list