tags:

views:

457

answers:

5

Hello,

Is there a difference and what type of between PAnsiChar and PChar? (in Delphi previous 2007)

Thank you, in advance!

A: 

PAnsiChar is pointer to non-unicode char (old string type), while PChar points to new style, unicode char.

Mihaela
You've been 44 seconds faster :)
Smasher
But you gave a better formulated answer :)
Mihaela
+8  A: 

In D2009 and later: yes, there is. PChar is a pointer to a Char which is a unicode character (a WideChar). And PAnsiChar is a pointer to a AnsiChar, which is - as the name implies - an ANSI character.

EDIT: For pre-2009 versions of Delphi PChar and PAnsiChar are exactly the same. They both point to an (Ansi) character.

Smasher
The question *specifically* states "Delphi previous 2007", which surely means "Delphi 2007 and earlier". This answer is simply flat out wrong in that context. PChar == PANSIChar in Delphi 2007 and earlier.
Deltics
Well, the question didn't state that when I answered it. You would have seen that if you had a look at the edit history before downvoting :(
Smasher
Well it does now, so correcting this answer in that context will help anyone else that comes to this question, see's your answer and goes away with completely the wrong answer in their head without thinking to check the edit history themselves.
Deltics
I already DID correct it, so I don't understand your problem. I think you're being a bit rough here. Besides: the answer is in no way "completely wrong" as I clearly point out that it is related to Delphi 2009 and later.
Smasher
A: 

Delphi 2009 Character types are Char, AnsiChar and WideChar, where Char defaults to WideChar. In previous versions of Delphi, a Char would be equivalent to an AnsiChar. The P is to indicate a pointer to the given type.

Darin Dimitrov
+1  A: 

PChar is a pointer to a "char", whatever that happens to be. In D2009 and later, Char means a UnicodeChar. Before that, Char was an AnsiChar.

The difference is that if you're using D2007 and migrate to a later version, the definition of PChar will change, while the definition of PAnsiChar will not.

Mason Wheeler
A: 

I take notice of the "In Delphi previous 2007" in your question, which I take to mean "In Delphi 2007 and earlier", so....

In Delphi 2007 and previous PChar and PANSIChar are synonymous. They mean the same thing - a pointer to an ANSIChar value. Char is synonymous with ANSIChar in those versions.

However, in Delphi 2009 and later, Char becomes synonymous with WideChar, so PChar becomes synonymous with PWideChar.

Note, that a WideChar is NOT a Unicode character - Unicode is simply not that straightforward. Any one WideChar value may be a codepoint in the BMP (Basic Multilingual Plane) or it may be either one of a surrogate-pair. It may also be a diacritical mark - for example an "accent" to be applied to the immediately preceding codepoint in a WideString.

The concept of a "character" in Unicode does not easily map to a single value of any type.

Deltics
A so called `ANSIChar` isn't necessarily an Ansi character either. It can as well be the leading or trailing byte of an MBCS character.
mghie
Compounding mistakes of the past is no way to ensure correct understanding in the future.
Deltics