views:

226

answers:

3

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

+6  A: 

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.

dthorpe
Actually, Inc() and Dec() style pointer math has been working for ages (see http://www.delphi3000.com/articles/article_2224.asp); Delphi 2009 now adds suport for arithmic in the MyPInt[index] style if you enable that directive (see http://rvelthuis.de/articles/articles-pointers.html).
Jeroen Pluimers
thanks for info ,is ther any chance that i can attain in delphi 7 ? :S
steve0
No, Steve. Delphi 7 was released about a decade ago, and there have been five subsequent releases. There will be no further releases of that version. What you have now is all you're ever going to have, unless you upgrade to a new version.
Rob Kennedy
Actually, PByte behave much like PChar on new Delphi with pointer math. "The fundamental type PByte represents a pointer to any byte data that is not character data. This type is declared with the {$POINTERMATH ON} compiler directive".
Alexander
@Alexander: Sure, in Delphi 2009 or later. I haven't used that version. The OP has Delphi 7, so PByte doesn't have benefit of that Delphi 2009 compiler directive.
dthorpe
I don't have an older version of Delphi to test it... But I guess instead of "P := P + X", it could be done "P := @P[X]" if pointermath isn't enabled for PByte. (Or any pointer to a 8-bits sized element)
Ken Bourassa
@Ken: Or just use Inc(P,X) as noted by Jeroen above.
dthorpe
+2  A: 
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.

PhiS
Define and start using a separate type to cast pointers to integers. Soon, a pointer won't fit in an integer anymore. (Due to 64-bit).
Marco van de Voort
@Marco - Yes, good point. There are NativeInt/NativeUInt in recent Delphi versions. I'll add that to my answer.
PhiS
Note that Inc and Dec only work on typed pointers, e.g. PByte, not on untyped (pointer). You may need to use something like `type NativeInt : LongInt;...o := Pointer(NativeInt(Data) + x * SizeOf(byte));`
Gerry
@Gerry - Yes. I do actually say _typed_ _pointers_. And yes, when you are doing the pointer math manually, of course you need to make sure you are adding the correct offsets.
PhiS
@PhiS: I intended my comment as a general *pointer* for people less familiar with Pascal. Mind upu it was late at night where I live
Gerry
A: 

PByte.

PChar.

Pointer types.

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?

Alexander
The existence of docwiki etc may not be immediately obvious to someone with only Delphi 7 (and need for as well, as D7 had a much faster help engine)
Gerry
Is there some problem with hitting F1 in Delphi 7 and reading the same topics in Delphi 7 help? They even contain information, which is relevant for your specific Delphi version!
Alexander