[Lazarus] Converting pchar to pcchar

Sven Barth pascaldragon at googlemail.com
Sun Aug 14 20:30:20 CEST 2011


On 14.08.2011 18:45, Alex du Plessis wrote:
> Hi List,
>
> Could anyone tell me what is the difference between a pchar and a
> pcchar? |And how does one create the one from the other.
>

If you look into the unit "ctypes" (located in $fpcdir/rtl/inc) you'll 
see that "ppchar" is defined as "^cchar" and "cchar" is defined as 
"cint8" which in turn is defined as "shortint". While SizeOf(ShortInt) = 
SizeOf(Char) is true ("Char" is the base of "PChar"), ShortInt=Char is not.

I personally would just cast one to the other:

=== source begin ===

var
   pcc: PCChar;
   pc: PChar;
begin
   pc := 'Hello World'; // I hope one can assign strings to PChars 
directly O.o
   pcc := PChar(pc);
end;

=== source end ===

> I have a library that was written in c++ and need to enter a string
> parameter into one of the (crucial) functions. When I used h2pas to
> convert the c header file it replaced the parameter type with a pcchar
> on that function. As far as I can determine, the two types are not
> compatible - the type checking in the compiler throws an error and
> refuses to compile.
>
> I could state the question differently as well. How do I get a string
> into a pcchar variable?

You first need to cast the String to PChar (so that the compiler's 
String->PChar magic can do its work) and then you can cast it to PCChar.

Regards,
Sven

PS: You might be better of posting such questions on fpc-pascal, as more 
compiler devs are there.




More information about the Lazarus mailing list