Board index » delphi » fill an array of pchar with char.

fill an array of pchar with char.

I tried to fill up an array of pchar with char.
like this

var
palabra, aux2:string
aux:pchar
arra: array [1..N] of pchar
//....
for i:=1 to m do begin
aux2:=palabra[t]+palabra[j]+palabra[k]+palabra[l];
aux:=str2pchar(aux2);
arra[i]:=aux;

function Str2PChar(var str: string): PChar;
begin
  Str[Length(Str)+1] := #0;
  str2pchar := @str[1];
end;

It doesn't seem to work and I tried a lot of variations but it does not
work. Does any one know why? Should this code work?

Thanks
mag

 

Re:fill an array of pchar with char.


Quote
In article <b174fs$sk...@news.ya.com>, Mikkel wrote:
> I tried to fill up an array of pchar with char.
> like this

> var
> palabra, aux2:string
> aux:pchar
> arra: array [1..N] of pchar
> //....
> for i:=1 to m do begin
> aux2:=palabra[t]+palabra[j]+palabra[k]+palabra[l];
> aux:=str2pchar(aux2);
> arra[i]:=aux;

> function Str2PChar(var str: string): PChar;
> begin
>   Str[Length(Str)+1] := #0;
>   str2pchar := @str[1];
> end;

> It doesn't seem to work and I tried a lot of variations but it does not
> work. Does any one know why? Should this code work?

What is a pchar? A pchar is a pointer to a char, nothing less, nothing more. It is
not a stringtype with which chars can be added with a "+" sign.

If you use some advanced compiler (FPC,VP,TMT), you could try to use the
ansistring type, but that will also take some reading up on string types.
(hint: name the compiler you are using in c.p.l.misc)

Re:fill an array of pchar with char.


"Marco van de Voort" <mar...@toad.stack.nl> escribi en el mensaje
news:slrnb3e6bb.v51.marcov@toad.stack.nl...

Quote
> In article <b174fs$sk...@news.ya.com>, Mikkel wrote:
> > I tried to fill up an array of pchar with char.
> > like this

> > var
> > palabra, aux2:string
> > aux:pchar
> > arra: array [1..N] of pchar
> > //....
> > for i:=1 to m do begin
> > aux2:=palabra[t]+palabra[j]+palabra[k]+palabra[l];
> > aux:=str2pchar(aux2);
> > arra[i]:=aux;

> > function Str2PChar(var str: string): PChar;
> > begin
> >   Str[Length(Str)+1] := #0;
> >   str2pchar := @str[1];
> > end;

> > It doesn't seem to work and I tried a lot of variations but it does not
> > work. Does any one know why? Should this code work?

> What is a pchar? A pchar is a pointer to a char, nothing less, nothing
more. It is
> not a stringtype with which chars can be added with a "+" sign.

> If you use some advanced compiler (FPC,VP,TMT), you could try to use the
> ansistring type, but that will also take some reading up on string types.
> (hint: name the compiler you are using in c.p.l.misc)

I use freepascal. www.freepascal.org

As it's declared > aux2:=palabra[t]+palabra[j]+palabra[k]+palabra[l];
is a string, not a pchar, In my modest opinion this is legal.
the conversion from string to pchar is done by the funcion
str2pchar();
aux is the pchar.

thanks for your time.

Re:fill an array of pchar with char.


Quote
Mikkel wrote:

> "Marco van de Voort" <mar...@toad.stack.nl> escribi en el mensaje
> news:slrnb3e6bb.v51.marcov@toad.stack.nl...
> > In article <b174fs$sk...@news.ya.com>, Mikkel wrote:
> > > I tried to fill up an array of pchar with char.
> > > like this

> > > var
> > > palabra, aux2:string
> > > aux:pchar
...
> > > function Str2PChar(var str: string): PChar;
> > > begin
> > >   Str[Length(Str)+1] := #0;
> > >   str2pchar := @str[1];
> > > end;

> > > It doesn't seem to work and I tried a lot of variations but it does not
> > > work. Does any one know why? Should this code work?

> > What is a pchar? A pchar is a pointer to a char, nothing less, nothing more. It is
> > not a stringtype with which chars can be added with a "+" sign.

> I use freepascal. www.freepascal.org

> As it's declared > aux2:=palabra[t]+palabra[j]+palabra[k]+palabra[l];
> is a string, not a pchar, In my modest opinion this is legal.

You are right.

Quote
> the conversion from string to pchar is done by the funcion
> str2pchar();

At first you should use "StrPCopy(destination: PChar; source: String)"
instead of your own Str2PChar. (Be sure that destination is big enough!)
The problem with your function is that it doen't copy the string. It
only takes a reference to the first char of the string "str". Your
functions only works correctly if you don't modify "str" after calling
Str2PChar.

But because you are using FreePascal, I recommend using "AnsiString"s.
No need for PChars. And if you really need a PChar, you can simply use
@ansistring[1] - no need to add a trailing #0 with AnsiStrings.

Wolf

Re:fill an array of pchar with char.


"Marco van de Voort" <mar...@toad.stack.nl> escribi en el mensaje
news:slrnb3e6bb.v51.marcov@toad.stack.nl...

Quote
> In article <b174fs$sk...@news.ya.com>, Mikkel wrote:
> > I tried to fill up an array of pchar with char.
> > like this

> > var
> > palabra, aux2:string
> > aux:pchar
> > arra: array [1..N] of pchar
> > //....
> > for i:=1 to m do begin
> > aux2:=palabra[t]+palabra[j]+palabra[k]+palabra[l];
> > aux:=str2pchar(aux2);
> > arra[i]:=aux;

> > function Str2PChar(var str: string): PChar;
> > begin
> >   Str[Length(Str)+1] := #0;
> >   str2pchar := @str[1];
> > end;

> > It doesn't seem to work and I tried a lot of variations but it does not
> > work. Does any one know why? Should this code work?

> What is a pchar? A pchar is a pointer to a char, nothing less, nothing
more. It is
> not a stringtype with which chars can be added with a "+" sign.

> If you use some advanced compiler (FPC,VP,TMT), you could try to use the
> ansistring type, but that will also take some reading up on string types.
> (hint: name the compiler you are using in c.p.l.misc)

I use ansistring and it works, you have saved my life!!!
I really don't understand why it doesn't work with pchar, I think I was
doing legal operations, but thanks!!!!!!!!
I can go on with my program I lost a day with this problem, and I only had
to use ansistrings instead of pchar!! thanks!!!!!

Re:fill an array of pchar with char.


Quote
In article <b177ll$vm...@news.ya.com>, Mikkel wrote:

>> > var
>> > palabra, aux2:string
>> > aux:pchar
>> > arra: array [1..N] of pchar
>> > //....
>> > for i:=1 to m do begin
>> > aux2:=palabra[t]+palabra[j]+palabra[k]+palabra[l];
>> > aux:=str2pchar(aux2);
>> > arra[i]:=aux;

>> > function Str2PChar(var str: string): PChar;
>> > begin
>> >   Str[Length(Str)+1] := #0;
>> >   str2pchar := @str[1];
>> > end;

>> > It doesn't seem to work and I tried a lot of variations but it does not
>> > work. Does any one know why? Should this code work?

>> What is a pchar? A pchar is a pointer to a char, nothing less, nothing
> more. It is
>> not a stringtype with which chars can be added with a "+" sign.

>> If you use some advanced compiler (FPC,VP,TMT), you could try to use the
>> ansistring type, but that will also take some reading up on string types.
>> (hint: name the compiler you are using in c.p.l.misc)

> I use ansistring and it works, you have saved my life!!!
> I really don't understand why it doesn't work with pchar, I think I was
> doing legal operations, but thanks!!!!!!!!

You were doing legal operations, the problem is the loop. You create a pointer that pokes
somewhere inside aux2, and in the next iteration, you overwrite aux2.

And I indeed had overread some things and confused aux with aux2 in the first msg, sorry.

Quote
> I can go on with my program I lost a day with this problem, and I only had
> to use ansistrings instead of pchar!! thanks!!!!!

Ansistrings are nice. They are easy, versatile (no 255 char limit and
typecastable to pchar), and have very low overhead.

Other Threads