[fpc-pascal] Pointers

Frank Peelo f26p at eircom.net
Fri Feb 12 09:13:47 CET 2010

Jonas Maebe wrote:
> On 11 Feb 2010, at 18:17, Rainer Stratmann wrote:
>> In the past with the turbopascal compiler and other always sizeof byte 
>> was
>> added.
> That is not true. This program prints "2" when compiled under Turbo Pascal:
> {$t-}
> type
>   pw = ^word;
> var
>   w: pw;
> begin
>   w:=nil;
>   inc(w);
>   writeln(longint(w));
> end.

i.e. incrementing a pointer to word changes the address by sizeof(word) 
= 2 bytes.

Incrementing always by sizeof(byte) just would not make sense. If you 
have a type that takes more than one byte, such as word, or double, then 
incrementing by sizeof(byte) would take you *inside* the variable, to an 
address where you don't know what you should find! It would depend on 
the endianness of the CPU, and what was in memory after the end of the 
variable, and stuff like that. The only sensible way to increment a 
pointer to SomeType is to increment by sizeof(SomeType). If you really 
want to pick apart a variable byte by byte, use ^Byte.


More information about the fpc-pascal mailing list