From Lazarus wiki
Revision as of 12:00, 27 August 2021 by Kai Burghardt (talk | contribs) (add more links, add note concerning constant assignment, mention pAnsiChar)
Jump to navigationJump to search


The data type pChar is a pointer to a single char.

	pChar = ^char;

PAnsiChar is an alias for pChar.

special support

A pChar could be interpreted as a pointer to NULL-terminated string data type value on the heap. This means this string value ends with chr(0). This kind of storing data is rather unsafe and Pascal’s own string data types always indicate an explicit (maximum) length. However, many libraries written in other programming languages make use of this dangerous data type and in order to facilitate interaction with them the FPC provides special support (only available in non-ISO modes).

direct assignments

A string literal can be assigned directly to a pChar:

program pCharDemo(output);
	p: pChar;
	p := 'Foobar';

This assigns p the address of an invisible constant value. That means you are not allowed to alter any component of this value. (Enforced since FPC 3.0.0)


Write/writeLn can accept a pChar and take it as a null-terminated string (provided the destination is a text file). Conversely writeStr does not support pChar.

The standard run-time library provides many handy functions:

There is also {$modeSwitch pCharToString}/‑MpCharToString automatically interpreting pChar values as null-terminated strings and automatically converting them to the destination’s string data type.

data types

The ANSIString data type is a pointer to a char and always ends with a complimentary null-Byte which allows direct typecasting to pChar:

program ansiStringAsPcharDemo(output);
	s: ANSIString;
	p: pChar;
	s := 'foobar';
	p := pChar(s);

Beware that this works well only as long as you are merely reading from the typecasted value. Also note that an ANSIString is allowed to contain chr(0) as payload.


Note: If you intend to alter data via the pointer, you possibly want to call uniqueString before you do the typecast.


The pChar data type used as a null-terminated string data type is rather an anomaly in the Pascal world. It has many caveats:

  • The definition of char depends on {$modeSwitch unicodeStrings}, that means in FPC 3.2.0 it could either refer to a 1‑Byte or 2‑Byte quantity. Most noticeably, this affects the offset if you are using pointer arithmetic, e. g. inc(pCharVariable).
  • In Pascal a pChar is just that: a pointer to a single char value. See FPC Unicode support for more details.
    • That means the string concatenation operator + does not work. If {$pointerMath on} it will rather refer to arithmetic addition. You will need to use, for instance, strings.strCat instead.
    • Also the comparison operator = will in fact compare addresses. Use, for instance, strings.strComp to compare the referenced null-terminated string values, unless you really want to ensure two null-terminated strings are identical (= share the same memory).
  • Debugging is more difficult, confer FPDebug.

Using a pChar just as a regular pointer to a single char, however, does not have any special caveats.

see also