Marc Santhoff wrote: > Hi, > > the docs only show examples of macros without any parameters. Is it > possible to use them? > > Can I make this work anyhow: > > {$MACRO ON} > {$define HOFFSET(rec,field) := pointer(@rec.field) - pointer(@rec)} > > type > s1_t = record > a: longint; > b: single; > c: double; > end; > var > s1: s1_t; > BEGIN > s1.a := 12345; > s1.b := 1.000000001; > s1.c := 1.000000002; > > writeln(HOFFSET(s1, a)); { Line 32 in my code } > END. > > Written as it is the compiler doesn't like it: > > $ fpc offsetof > Free Pascal Compiler version 2.0.2 [2005/11/17] for i386 > Copyright (c) 1993-2005 by Florian Klaempfl > Target OS: FreeBSD/ELF for i386 > Compiling offsetof.pas > offsetof.pas(32,10) Error: Identifier not found "HOFFSET" > offsetof.pas(32,22) Error: Identifier not found "a" > offsetof.pas(32,25) Error: Illegal expression > > Other solutions are very welcome... >
Macros with parameters are not supported, AFAIK. I always workaround it by employing the fact that macros within macros are expanded. So instead of {$define HOFFSET(rec,field) := pointer(@rec.field) - pointer(@rec)} you can define parameter-less macro like {$define HOFFSET := pointer(@HOFFSET_rec.HOFFSET_field) - pointer(@HOFFSET_rec)} (I prefix macro params with "HOFFSET_" just for safety, you could as well just use original "rec" and "field" identifiers). And then to use it, instead of writeln(HOFFSET(s1, a)); you can write {$define HOFFSET_rec := s1} {$define HOFFSET_field := a} writeln(HOFFSET); This way you get behavior of macros with parameters. Admittedly, this looks clumsy, but at least works. Michalis _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal