next up previous contents index
Next: Booleans Up: Data types Previous: Real 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.

Strings

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

Var
   S : String[Size];
This will declare S as a variable of type String, with maximum length Size. Size can be any value from 1 to 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.

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:

S:='This is a string.';
S:='One'+', Two'+', Three';
S:='This isn''t difficult !';
S:='This is a weird character : '#145' !';
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:

program one;

var p : pchar;

begin
  P:='This is a null-terminated string.';
  writeln (P);
end.

Results in the same as

program two;

const P : PChar = 'This is a null-terminated string.'

begin
  Writeln (P);
end.

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:

Program three;

Var S : String[30];
    P : Pchar;

begin
  S:='This is a null-terminated string.'#0;
  P:=@S[1];
  writeln (P);
end.

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 strings unit.

However, it is possible to do some pointer arithmetic. You can use the operators + and - to do operations on PChar pointers. In table (1.3), 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 1.3: PChar pointer arithmetic


next up previous contents index
Next: Booleans Up: Data types Previous: Real types

Michael Van Canneyt
Thu Sep 10 14:02:43 CEST 1998