Board index » delphi » pchar := string; string:=pchar

pchar := string; string:=pchar

var a:string;
    b:pchar;
Begin
  a:=b;
  b:=a;
End.

This think does not work. How kan I make it work?

Tomaz

 

Re:pchar := string; string:=pchar


Quote
Tomaz Gregorec wrote:

> var a:string;
>     b:pchar;
> Begin
>   a:=b;
>   b:=a;
> End.

> This thing does not work. How kan I make it work?

Tomaz!

PChar is only a pointer. You need some memory where it
shall point to. You can obtain heap memory, but it is
not necessary in all cases.

PChar is not a pointer to a string, but to a "C" type
array of characters, without a leading length byte but
with a trailing #0 character to fix the string length.

BPW 7 and Delphi provide special functions to convert
a Pascal-type string to a C-type Pchar:

Var T : Array[0..255] of Char;

  b := StrPcopy(T,a);
will fill the array at T with the characters from string
a in the proper manner and deliver a pointer result, in
this case Addr(T).

You must make sure that T is alive as long as b is used
in some way!

You can use some GetMem for b to obtain the necessary
memory from the heap. But it must be returned after use.

For the opposite direction look for the StrPas function
in your F1 - help.

Regards, Franz Glaser

Other Threads