[fpc-pascal] Porting C macro

Darius Blaszyk dhkblaszyk at zeelandnet.nl
Tue Mar 12 09:30:04 CET 2013


 

Flávio Etrusco schreef op 12 mrt '13: 

> On 3/11/13, Darius Blaszyk
<dhkblaszyk at zeelandnet.nl> 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)))) Does anyone know what is
meant with the define? Obviously LList is a linked list struct that has
a prev and next variable. But what does the & and 0 mean? Appreciate any
help. Regards, Darius
> 
> AFAICS they mean "address" and "nil",
respectively, so that part will
> return the offset of "next" inside and
item of LList.
> 
> -Flávio

So here is what I made based on your and
Henry's mail. However still it does not seem to work, although other
ported code might be at fault of course :)

//#define GETNEXT(x) ((LList
*)(((char *) x) - ((char *) & (((LList *)0)->.next))))
function
GETNEXT(x: pLList): pLList; inline;
begin
 exit(pLList(pchar(x) -
pchar(pLList(nil)^.next)));
end; 

And let LList be declared as:

LList
= record
 len: integer;
 next, prev: pLList;
 name: PChar;
 nextname:
PChar;
 flag: integer;
end; 

Then I still don't understand what this
does: 
 if list^.next <> nil then
 list^.nextname :=
GETNEXT(list^.next)^.name;

Regards, Darius 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130312/60d0cbe5/attachment.html>


More information about the fpc-pascal mailing list