next up previous contents index
Next: Structured Types Up: Types Previous: Base types

Character types

Char

Free Pascal supports the type Char. A Char is exactly 1 byte in size, and contains one character.

You can specify a character constant by enclosing the character in single quotes, as follows : 'a' or 'A' are both character constants.

You can also specify a character by their ASCII value, by preceding the ASCII value with the number symbol (#). For example specifying #65 would be the same as 'A'.

Also, the caret character (^) can be used in combination with a letter to specify a character with ASCII value less than 27. Thus ^G equals #7 (G is the seventh letter in the alphabet.)

If you want to represent the single quote character, type it two times successively, thus '''' represents the single quote character.

Short Strings

Free Pascal supports the String type as it is defined in Turbo Pascal. To declare a variable as a string, use the following type specification:


ShortString

syntdiag1402

The predefined typeShortString is defined as a string of length 255.

Free Pascal reserves Size+1 bytes for the string S, and in the zeroeth element of the string (S[0]) it will store the length of the variable.

If you don't specify the size of the string, 255 is taken as a default.

For example in
listing1290
NameString can contain maximum 10 characters. While StreetString can contain 255 characters. The sizes of these variables are, respectively, 11 and 256 bytes.

To specify a constant string, you enclose the string in single-quotes, just as a Char type, only now you can have more than one character. Given that S is of type String, the following are valid assignments:
listing1297
As you can see, the single quote character is represented by 2 single-quote characters next to each other. Strange characters can be specified by their ASCII value.

The example shows also that you can add two strings. The resulting string is just the concatenation of the first with the second string, without spaces in between them. Strings can not be substracted, however.

PChar

Free Pascal supports the Delphi implementation of the PChar type. PChar is defined as a pointer to a Char type, but allows additional operations.

The PChar type can be understood best as the Pascal equivalent of a C-style null-terminated string, i.e. a variable of type PChar is a pointer that points to an array of type Char, which is ended by a null-character (#0).

Free Pascal supports initializing of PChar typed constants, or a direct assignment. For example, the following pieces of code are equivalent:


listing1308
Results in the same as
listing1310

These examples also show that it is possible to write the contents of the string to a file of type Text.

The strings unit contains procedures and functions that manipulate the PChar type as you can do it in C.

Since it is equivalent to a pointer to a type Char variable, it is also possible to do the following:
listing1316
This will have the same result as the previous two examples.

You cannot add null-terminated strings as you can do with normal Pascal strings. If you want to concatenate two PChar strings, you will need to use the unit strings.

However, it is possible to do some pointer arithmetic. You can use the operators + and - to do operations on PChar pointers. In table (gif), P and Q are of type PChar, and I is of type Longint.

  

Operation Result
P + I Adds I to the address pointed to by P.
I + P Adds I to the address pointed to by P.
P - I Substracts I from the address pointed to by P.
P - Q Returns, as an integer, the distance between 2 addresses
(or the number of characters between P and Q)
Table: PChar pointer arithmetic


next up previous contents index
Next: Structured Types Up: Types Previous: Base types

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998