title explains all
and why i cant perform this operation
var
data:pbyte;
x:int64;
o:pointer;
begin
o:=data+x;
end;
thanks in advance
regards
title explains all
and why i cant perform this operation
var
data:pbyte;
x:int64;
o:pointer;
begin
o:=data+x;
end;
thanks in advance
regards
PChar is a pointer to char, but it receives special support from the compiler to allow pointer arithmetic to make C-like string manipulations easier in Delphi. PByte is just a plain old typed pointer, and does not receive any special attention from the compiler to allow pointer arithmetic.
In Delphi 2009, a new compiler directive was introduced ($POINTERMATH ON/OFF) which allows you to add compiler support for pointer arithmetic to your own pointer type declarations.
pbyte = ^byte;
pchar = ^char;
In old Delphi versions (prior to D2009), SizeOf(char)
=SizeOf(byte)
, i.e., 8-bit.
In D2009 and later, char
is 16-bit whereas byte
remains 8-bit, so that:
SizeOf(byte)=1
SizeOf(char)=2
To allow modifying pointers by e.g. adding values etc., you can use $POINTERMATH ON
(available in D2009 and later, see here). The alternative is to follow the pattern:
NewPointer:= Pointer(Integer(OldPointer)+IntegerValue)
Edit1 -- Note that (as pointed out in comments to another answer), also inc()
and dec()
work with typed pointers; they will increment/decrement a PMyType by SizeOf(TMyType).
Edit2 -- For future-proofing your code, you should consider that SizeOf(Pointer) will probably change in future 64-bit Delphi versions, so that the relationship SizeOf(Integer)=SizeOf(Pointer)
will no longer hold. To circumvent this, recent Delphi versions define the types NativeInt
and NativeUInt
, which are integers that have the same size as a pointer
.
Using PByte instead of PChar, when moving to Unicode.
Seriosly, what's the point of posting trivial question, wait few days to get answer, if you may just open and read help and get your answer just in few minutes?