Board index » delphi » Null Terminating Char arrays HELP!!!!!!!!!!!!

Null Terminating Char arrays HELP!!!!!!!!!!!!

I would like to know why i can't initialize a null terminating char array to
chr(0) in position 0 in the below circumstances.

I want to be able to store a string in the array with SendDlgItemMsg, and
em_GetLine in an edit field.  But if I initialize the first char of the
array to char 0, it won't store the string in the array.  It is almost as if
the variable's address is screwed up or something.

Any help would be greatly appreciated.  Thanks.  Oh, I would also like a
reply by email please
ada...@telusplanet.net Thanks

 

Re:Null Terminating Char arrays HELP!!!!!!!!!!!!


On Tue, 28 Oct 1997 23:42:08 -0700, "|0|" <ada...@telusplanet.net>
wrote:

Quote
>I would like to know why i can't initialize a null terminating char array to
>chr(0) in position 0 in the below circumstances.

>I want to be able to store a string in the array with SendDlgItemMsg, and
>em_GetLine in an edit field.  But if I initialize the first char of the
>array to char 0, it won't store the string in the array.  It is almost as if
>the variable's address is screwed up or something.

>Any help would be greatly appreciated.  Thanks.  Oh, I would also like a
>reply by email please
>ada...@telusplanet.net Thanks

From the description above I cannot see exactly what you are trying to
do, but I think the problem may be caused by the fact that an empty
string of type Pchar (Borland Pascal's zero-ended string type) is
usually represented by a nil pointer.

If you declare
  var  MsgPtr: PChar;
you only have a pointer variable and should allocate it to some actual
string before using it.

But if you declare
  var Msg: array[0..80] of char;
the statement 'Msg[0]:= #0' will effectively clear the string.

Hope this helps,
Bob Ferguson.

--
J.R. Ferguson, Amsterdam, The Netherlands
e-mail: j.r.fergu...@iname.com
web: www.xs4all.nl/~ferguson
Freeware for DOS and Windows

Re:Null Terminating Char arrays HELP!!!!!!!!!!!!


In article <636lrv$...@priv-sys04-le0.agt.net>, ada...@telusplanet.net says...

Quote
>I would like to know why i can't initialize a null terminating char array to
>chr(0) in position 0 in the below circumstances.

The Strings unit has a StrCopy procedure for this:

uses strings;
var s: PChar;
begin
  getmem(s,1024); { the max string length you want }
  strcopy(s,'');
  ...
end.

I found this weekend a strange effect:
I can index a PChar variable as standard strings!!!
The s[0] := #0 works! (BP7)

-BandiT-

Re:Null Terminating Char arrays HELP!!!!!!!!!!!!


(Posted and emailed)
Deleted cross-post to obsolete  "comp.lang.pascal"       ...red

Quote
"|0|" <ada...@telusplanet.net> wrote:
>I would like to know why i can't initialize a null terminating char array to
>chr(0) in position 0 in the below circumstances.

>I want to be able to store a string in the array with SendDlgItemMsg, and
>em_GetLine in an edit field.  But if I initialize the first char of the
>array to char 0, it won't store the string in the array.  It is almost as if
>the variable's address is screwed up or something.

Captain, there be a little "C" leakage here.  :-D

I assume you are using StrNew to allocate delimited strings on
the heap.  Online help for StrNew states:
   If Str is nil or points to an empty string, StrNew
   returns nil and does not allocate any heap space.

The same is true when you use NewStr to allocate pStrings.

In "C", NULL pointers are used to represent empty strings.  This
concept was adopted when zero delimited strings, various string
collections and C-type window objects were added to TP/BPW. :(

BTW, de-referencing a NIL pointer will generate a GPF in
protected mode and return the contents of int 0 vector in real
mode.

    ...red

--
Support the anti-Spam amendment
  Join at http://www.cauce.org/

Re:Null Terminating Char arrays HELP!!!!!!!!!!!!


On 29 Oct 1997 11:10:31 GMT, apa...@mol.hu (BandiT) wrote:

Quote
>uses strings;
>var s: PChar;
>begin
>  getmem(s,1024); { the max string length you want }
>  strcopy(s,'');
>  ...
>end.

>I found this weekend a strange effect:
>I can index a PChar variable as standard strings!!!
>The s[0] := #0 works! (BP7)

Strange effect ? You discovered the main feature of this data type. Do
you never copy the documentation when you pirate software ?

SCNR

Regards
Horst

Other Threads