Board index » delphi » PChar is *not* ^Char.

PChar is *not* ^Char.

Hi there,

In all the documents and on-line help that I read about Delphi, it
says that type PChar is a pointer to Char:

  type PChar = ^Char

See for instance the on-line help on 'Type PChar'.

Well, no so! The following code shows that this is not true:

  type
      MyPChar1 = PChar;
      MyPChar2 = ^Char;
  var
      Str1: MyPChar1;
      Str2: MyPChar2;
  begin
      { Allocate memory for both Str1 and Str2 (not shown here)! }

      Str1[3] := 'a';    { this line is ok }
      Str2[3] := 'a';    { "Error 121: invalid qualifier" }
  end.

So, what is the *real* definition of PChar?

Bye,

-- Gerben

 

Re:PChar is *not* ^Char.


Quote
abb...@nici.kun.nl (Gerben Abbink) writes:
> Hi there,

> In all the documents and on-line help that I read about Delphi, it
> says that type PChar is a pointer to Char:

>   type PChar = ^Char

> See for instance the on-line help on 'Type PChar'.

> Well, no so! The following code shows that this is not true:

>   type
>       MyPChar1 = PChar;
>       MyPChar2 = ^Char;
>   var
>       Str1: MyPChar1;
>       Str2: MyPChar2;
>   begin
>       { Allocate memory for both Str1 and Str2 (not shown here)! }

>       Str1[3] := 'a';    { this line is ok }
>       Str2[3] := 'a';    { "Error 121: invalid qualifier" }
>   end.

> So, what is the *real* definition of PChar?

> Bye,

> -- Gerben

No, its really a pointer to an array of char.

Regards,
Don.

Re:PChar is *not* ^Char.


Quote
In article <42amb8$...@inet-nntp-gw-1.us.oracle.com> dbeu...@us.oracle.com (Don Beusee) writes:
>abb...@nici.kun.nl (Gerben Abbink) writes:
>> In all the documents and on-line help that I read about Delphi, it
>> says that type PChar is a pointer to Char:

>>   type PChar = ^Char

>> See for instance the on-line help on 'Type PChar'.

>> Well, no so! The following code shows that this is not true:
 ...
>No, its really a pointer to an array of char.

No, that's not right either.  If it was, you'd be allowed to use p^[i], not
p[i].  

In plain $X- mode, PChar really is just ^Char.  However, in $X+ mode, it's
treated in special ways by the compiler that aren't achievable by any type
declaration.  See the Language Reference for the details. (If you didn't buy a
copy, download one from www.borland.com.)

Duncan Murdoch

Re:PChar is *not* ^Char.


Quote
dmurd...@mast.queensu.ca (Duncan Murdoch) writes:
>In article <42amb8$...@inet-nntp-gw-1.us.oracle.com> dbeu...@us.oracle.com (Don Beusee) writes:

>>abb...@nici.kun.nl (Gerben Abbink) writes:

>>> In all the documents and on-line help that I read about Delphi, it
>>> says that type PChar is a pointer to Char:

>>>   type PChar = ^Char

>>> See for instance the on-line help on 'Type PChar'.

>>> Well, no so! The following code shows that this is not true:
> ...
>>No, its really a pointer to an array of char.

>No, that's not right either.  If it was, you'd be allowed to use p^[i], not
>p[i].  

I was talking about semantics, not syntax.  A PChar IS a pointer to a char
array.  The $X+ extends the *syntax* to support PChar.  If you read the help
page on PChar carefully, you will see that I am right.

Without extended syntax, you need to define your own PChar type so that the
syntax matches the semantics.  Like this:

{ put these type statements in a shared unit if you like }
type
   MyCharArray: Array [0..65534] of char; { only defined for MyPChar }
   MyPChar: ^MyCharArray;

{ now use it }
var
   a: Array [0..79] of char;
   p: MyPChar;
begin
   p := @a;
   { note that PChar(p) is a direct cast - to satisfy the compiler }
   if (StrPCopy(PChar(p), 'copy this to the char array') <> PChar(p)) then
        p^[0] := #0; { error? }
end;

Quote
>In plain $X- mode, PChar really is just ^Char.  However, in $X+ mode, it's
>treated in special ways by the compiler that aren't achievable by any type
>declaration.  See the Language Reference for the details. (If you didn't buy a
>copy, download one from www.borland.com.)

Thats where PChar falls short.

Borland really did not need to do this hack (extended syntax).  They could
have just defined PChar the same way I defined MyPChar above.  I'd gladly
use standard Pascal syntax (which only requires one more character).

Regards,
Don.

Re:PChar is *not* ^Char.


Quote
In article <42eqeg$...@inet-nntp-gw-1.us.oracle.com> dbeu...@us.oracle.com (Don Beusee) writes:
>>In plain $X- mode, PChar really is just ^Char.  However, in $X+ mode, it's
>>treated in special ways by the compiler that aren't achievable by any type
>>declaration.  See the Language Reference for the details. (If you didn't buy a
>>copy, download one from www.borland.com.)
>Thats where PChar falls short.
>Borland really did not need to do this hack (extended syntax).  They could
>have just defined PChar the same way I defined MyPChar above.  I'd gladly
>use standard Pascal syntax (which only requires one more character).

I think the main things they wanted to achieve were to allow you to use a
string constant or a zero-based char array as an argument to a function
expecting a PChar.  Once they were in that state of sin, the rest followed
easily.

Duncan Murdoch

Re:PChar is *not* ^Char.


Quote
In article <mac.234.304DA...@zeus.hsrc.ac.za> m...@zeus.hsrc.ac.za (Malcolm Coulter) writes:
>In article <dmurdoch.2780.3049A...@mast.queensu.ca> dmurd...@mast.queensu.ca
>(Duncan Murdoch) writes:
>>In plain $X- mode, PChar really is just ^Char.  However, in $X+ mode, it's
>>treated in special ways by the compiler that aren't achievable by any type
>>declaration.  See the Language Reference for the details. (If you didn't buy a
>>copy, download one from www.borland.com.)
>-----------------------------------------------
>Which is why some people have such trouble with PCHAR in C.
>Is pStr (or pString depending where you are in Help) exactly the same as PCHAR
>or is it just a well-behaved pointer to a null-terminated string?

I don't think so, but you'll have to give the exact reference to be sure.  As
far as I know pString is ^String, i.e. a pointer to a counted string (*not*
null-terminated).

Duncan Murdoch

Re:PChar is *not* ^Char.


Quote
In article <dmurdoch.2780.3049A...@mast.queensu.ca> dmurd...@mast.queensu.ca (Duncan Murdoch) writes:

[cut]

Quote
> ...
>>No, its really a pointer to an array of char.
>No, that's not right either.  If it was, you'd be allowed to use p^[i], not
>p[i].  
>In plain $X- mode, PChar really is just ^Char.  However, in $X+ mode, it's
>treated in special ways by the compiler that aren't achievable by any type
>declaration.  See the Language Reference for the details. (If you didn't buy a
>copy, download one from www.borland.com.)

-----------------------------------------------
Which is why some people have such trouble with PCHAR in C.

Is pStr (or pString depending where you are in Help) exactly the same as PCHAR
or is it just a well-behaved pointer to a null-terminated string?

Re:PChar is *not* ^Char.


Quote
abb...@nici.kun.nl (Gerben Abbink) wrote:
>Hi there,
>In all the documents and on-line help that I read about Delphi, it
>says that type PChar is a pointer to Char:
>  type PChar = ^Char
>See for instance the on-line help on 'Type PChar'.
>Well, no so! The following code shows that this is not true:
>  type
>      MyPChar1 = PChar;
>      MyPChar2 = ^Char;
>  var
>      Str1: MyPChar1;
>      Str2: MyPChar2;
>  begin
>      { Allocate memory for both Str1 and Str2 (not shown here)! }
>      Str1[3] := 'a';    { this line is ok }
>      Str2[3] := 'a';    { "Error 121: invalid qualifier" }
>  end.
>So, what is the *real* definition of PChar?

I think PChar is an emulation of C's PChar and have no exact PASCAL
equivalent. For example, MyPChar1+1 is a valid expression which as far
as I know is illegal for any other pointer types.

Regards
Wai Wong.

Other Threads