Mega Code Archive

 
Categories / Delphi / Examples
 

Working with pchars

In this example we will look into the use of PChars. What is a PChar? What is it good for? Can't I manage with strings? PChars can be seen on as an array of characters, terminated by a zero (#0). The basic difference between Strings and PChars is that PChars is pointers. Operations on PChars will then be pointer-operations! Thats why we have to use special functions to perform tasks such as compare and setting of PChars. As we will see, there will be times when using PChars would be an advantage. Another strong reason to get to know about PChars, is that Windows is build with them ! (s:string, p:pchar in all examples) To use PChar you always must allocate some sort of memory where to perform the operations. Either you are given the memory by a called function, or a memory block defined in compilation-time (in stack or in the global data memory), or you must allocate it from the operating system. A few examples of how this can be done: 1) --- use heap memory { because long strings are implicitly nullterminated } { and dynamically allocated, the use of StrAlloc } { and StrDispose is in decline. } procedure test; var p:pchar; begin p:=stralloc(20); { can hold 19 characters } .... strdispose(p); { release memory again } end; 2) --- use stack- (or data-, if defined global) memory { #A - orginal, easy to understand } procedure test; var ch:Array[0..19] of char; begin p:=@ch[0]; end; { #B - as a constant, will automaticly be #0 - terminated } const a_pchar: pchar = 'anything you like'; { #C - make a string to a pchar, using string's allocated memory } procedure test; var s:string; begin s:='this is a test'; s:=s+#0; { makes string null -terminated } p:=@s[1]; { skip s[0], as it contains length info} end; There is serveral other methods. Another example: var P: PChar; ... P := 'Hello world!'; points P to an area of memory that contains a null-terminated copy of "Hello world!" This is equivalent to: const TempString: array[0..12] of Char = 'Hello world!'#0; var P: PChar; ... P := @TempString; To use the pchars, you mostly work in a simular way as with Strings, but using other function/procedure calls. strpos - pos strlen - length PChar-operations are REALLY different from those using Strings, they MUST be performed by function calls. s:='this is a string'; With PChars use: strnew - allocates new memory for copy strcopy - you supply a memory where to place copy strLcopy - as strcopy, but gives you the possibility to avoid copying 789 characters to your 512 bytes buffert (=crash). if s=s2 then With PChars use: strcomp - just as easy, "if strcomp(s,s2) then" strIcomp - same as above, none casesensitive. s:=s+s2; With PChars use: strcat - add another pchar to the end of one. strLcat - as strcat, (compare with strLcopy) You can find information about every single function in the online manuals. Often you will have to make a string to a PChar, or the other way around. (pchar -> string ) s:=strpas(p); (string -> pchar #1 ) strPcopy(p,s); { also strPLcopy is available, p must have allocated mem } (string -> pchar #2 ) s:=s+#0; p:=@s[1]; Say p points to an area containing 'hello world'#0. If you perform p:=p+3; p would point to: 'lo world'#0. As most functions returns a new pchar pointer (i.e strpos) to a location inside your memory, this can be very useful. If you wish to know how many character there is in '12345'#0 before the '4' you could not just use strpos, as it would return a pointer to the '345'#0, you would have to do something like this: no_chars_befor_4:= strpos('4',p) - p; This kind of operations can not be performed on a string. s:=s+1; would generate a nice compiler error. These pointer-operations is why PChar is a very nice tool working with parsing tasks !