From PChar to PByte

If this problem is potentially troublesome, at least for a lot of low-level code, the solution is at hand. In the code above, you can simply replace the version-specific PChar type with the version-agnostic PByte type61. A pointer to byte, in fact, remains the same and behaves the same regardless of the size of characters. All you have to do in a method like the one above is to change the pointer variable declaration to: var pOnelnteger: PByte;

Without changing anything else in the code, recompile the program and it should work. The good thing is that (in Delphi 2009) PByte supports the same pointer math that PChar supports. In past versions of Delphi, PByte

61 An alternative solution, which is more compatible with past versions of Delphi, is to use PAnsiChar rather than PChar. However, using PByte is generally recommended as it make your intent more clear and is more readable than using PAns iChar.

didn't support pointer math, but you could still use it in an algorithm like the one discussed here by changing the plus one with an increment call: Inc (pOnelnteger, 4);

That fact that Inc and Dec work with most pointer types is little known among Delphi users. Still, having the full pointer math means you can also compare pointers, and do other operations.

PInteger and the POINTERMATH Directive

Still, as we are dealing with Integers, wouldn't it be better to write the code like this (changing the increment to one and skipping one of the casts of the original code)?

procedure TFormPointerMath.btnPIntegerClick ( Sender: TObject);

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment