[fpc-pascal] How to get the offset of a record

Rainer Stratmann RainerStratmann at t-online.de
Sat Apr 30 15:15:32 CEST 2011


Am Saturday 30 April 2011 14:54:22 schrieb Aleksa Todorovic:
> On Sat, Apr 30, 2011 at 14:30, Rainer Stratmann
>
> <RainerStratmann at t-online.de> wrote:
> > In Turbopascal it was possible with offs()
> >
> > type
> >  rectype = record
> >  var1 : longint;
> >  var2 : longint;
> >  var3 : boolean;
> >  end;
> >
> > var
> >  o : longint;
> >  recvar : rectype;
> >
> >  o := offs( recvar.var1 );  // --> 0
> >  o := offs( recvar.var2 );  // --> 4
> >  o := offs( recvar.var3 );  // --> 8
>
> o := PtrUInt( @recvar.var1 ) - PtrUInt( @recvar );
> o := PtrIUnt( @recvar.var2 ) - PtrUInt( @recvar );
> o := PtrInUt( @recvar.var3 ) - PtrUInt( @recvar );

var addrx : longint;

type
 rectype = record
  var1 : longint;
  var2 : longint;
  var3 : boolean;
 end;
 rectype_2 = record
  ofs1 : longint;
  ofs2 : longint;
  ofs3 : longint;
 end;

var
 recvar : rectype;

procedure t;
var o1 , o2 , o3 : longint;
begin
 // works
 o1 := ptruint( @recvar.var1 ) - ptruint( @recvar );
 o2 := ptruint( @recvar.var2 ) - ptruint( @recvar );
 o3 := ptruint( @recvar.var3 ) - ptruint( @recvar );
end;

const rec : rectype_2 = (
 // does not work! (illegal expression)
 ofs1 : ptruint( @recvar.var1 ) - ptruint( @recvar );
 ofs2 : ptruint( @recvar.var2 ) - ptruint( @recvar );
 ofs3 : ptruint( @recvar.var3 ) - ptruint( @recvar );
 );


> > _______________________________________________
> > fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> > http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal





More information about the fpc-pascal mailing list