Roberto Freitas wrote:
> Hi, I supose it's something very easy, but I realy don't known how to 
> do it.
> P is a PAnsiChar variable and S is a String variable.
> This sentence is fine:
>  S := P;

That allocates a new string S and copies the memory pointed to by P into 
that string.

> But the inverse sentence is not:
>  P := S;
> (I get a compiler error message: Incompatible types: 'String' 
> and 'PAnsiChar')

PAnsiChar is a pointer to an AnsiChar. The compiler doesn't know which 
AnsiChar you want P to point at.

One way to do what you want is P := PAnsiChar(S). That makes P point to 
the first character of S. If S is empty, then P will point to a null 
character. Either way, P <> nil.

Another way is to use the StrLCopy function. It will copy the contents 
of S into the memory pointed to by P. To use that, P must already point 
somewhere that you're allowed to change. (That is, it can't point to 
read-only memory.)

> So, I try another track. It's possible to do:
>  P^ := '0';

That assigns the character 0 into the AnsiChar pointed to by P. The rest 
of the memory block P points to remains unchanged. If P doesn't point 
somewhere valid already, then you'll get a run-time error.

Also valid is P := '0'. That assigns P to point to the 0 character, so 
if P was pointing at something else already, that value is lost.

> But it's not possible to do:
>  for i := 1 to Length(S) do
>  P^ := Copy(S, i, 1);

First of all, if the third argument to Copy is 1, then Copy probably 
isn't the function you should call anyway. That kind of syntax is seen 
most often from people who've been using VB too long. To get a single 
character form a string, use the bracket operator: S[i].

The Copy function always returns a string, so your code fails for the 
same reason P := S failed.

> Conclusion: I need to append S to P but I don't known how to do it.
> Can somebody help me?

To append, you need to get to the end P. You can use the StrEnd function 
for that. It returns a PChar. Then use StrLCopy to copy S into that. The 
more direct function is StrLCat, which concatenates two PChars. In 
either case, you must be absolutely certain that the destination pointer 
points to a block of memory large enough to hold both strings. Delphi 
can't check that for PChars the way it can for strings.

-- 
Rob


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Fair play? Video games influencing politics. Click and talk back!
http://us.click.yahoo.com/u8TY5A/tzNLAA/yQLSAA/i7folB/TM
--------------------------------------------------------------------~-> 

-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to