Previous Next Contents Index Doc Set Home


Data Types

2


This chapter describes the Pascal data types. Some data types represent different values when you compile your program with or without the -xl option, and with or without the -calign option. The intent of the -xl option is to guarantee binary data compatibility between the operating system and Apollo MC680x0-based workstations. The intent of the -calign option is to improve compatibility with C language data structures.

This chapter contains the following sections:

Summary of Data Format Differences

page 10

real

page 13

Integer

page 16

boolean

page 20

Character

page 22

Enumerated Types

page 23

Subrange

page 25

Record

page 26

Array

page 34

Set

page 38

File

page 41

Pointer

page 41


Summary of Data Format Differences

A few data formats, particularly of structured types, change when you use the Pascal compiler -calign option, when you use the -xl option, and when you use the -calign with the -xl option. This section describes the data alignments and sizes that change with these options. See the remainder of the chapter for information on types that do not change when you use these options.

All simple data types take their natural alignments. For example, real numbers, being four-byte values, have four-byte alignment. Naturally, no padding is needed for simple types.

Default Data Alignments and Padding

Here is a summary of the default data alignments and padding.

Records

The alignment of a record is always four bytes. Elements take their natural alignment, but the total size of a record is always a multiple of four bytes.

Packed Records

Elements of types enumerated, subrange, integer16, and sets with a cardinal number less than 32 are bit-aligned in packed records.

Variant Records

The alignment of each variant in a record is the maximum alignment of all variants.

Arrays

The alignment of a array is equal to the alignment of the elements, and the size of most arrays is simply the size of each element times the number of elements. The one exception to this rule is that the arrays of aggregates always have a size that is a multiple of four bytes.

Sets

Sets have an alignment of four bytes when they are longer than 16 bits; otherwise, their alignment is two bytes. The size of a set is always a multiple of two bytes.

Enumerated Types

The size and alignment of enumerated types can be one byte or two, depending on the number of elements defined for the type.

Subranges

The size and alignment of subrange types varies from one to four bytes, depending on the number of bits requires for its minimum and maximum values. See Table 2-7 on page 26 for examples.

Data Formats with -calign-

With the -calign option, the data formats are:

Records

The alignment of a record is equal to the alignment of the largest element.

Packed Records

Packed records are the same as the default, except integer elements are not bit-aligned.

Arrays

The size of all arrays is the size of each element times the number of elements.

Sets

Sets have an alignment of two bytes. The size is the same as the default.

Data Formats with -xl-

In addition to the structured types discussed below, two simple data types change their sizes with the -xl option:

Packed Records

Values of type real have four-byte sizes and alignment. Values of type integer have a size of two bytes and are bit-aligned.

Enumerated Types

The size and alignment of enumerated types is always two bytes.

Subranges

The size and alignment of subrange types varies from two to four bytes, depending on the number of bits requires for its minimum and maximum values. See Table 2-7 for examples.

Data Formats with -calign and -xl-

When you use -xl with -calign, alignments and padding are the same as with -xl alone, with the following differences:

Arrays

Arrays are the same as with -calign alone, except the size of an array of booleans is always a multiple of two.

Varying Arrays

Varying arrays have an alignment of four bytes. The size is a multiple of four.


real

Pascal supports the standard predeclared real data type. As extensions to the standard, Pascal also supports:

real Variables

The minimum and maximum values of the real data types are shown in Table 2-1.

Table  2-1 real Data Types

Type
Bits
Maximum Value
Minimum Value

real (with -xl option)

32

3.402823e+38

1.401298e-45

real (without -xl option)

64

1.79769313486231470e+308

4.94065645841246544e-324

single

32

3.402823e+38

1.401298e-45

shortreal

32

3.402823e+38

1.401298e-45

double

64

1.79769313486231470e+308

4.94065645841246544e-324

longreal

64

1.79769313486231470e+308

4.94065645841246544e-324

This example declares five real variables:

var 	x: real;
	y: shortreal;
	z: longreal;
	weight: single;
	volume: double;

real Initialization

To initialize a real variable when you declare it in the var declaration of your program, create an assignment statement as follows:

This example initializes the variable ph to 4.5 and y to 2.71828182845905e+00.

var
	ph: single := 4.5; 
	y: longreal := 2.71828182845905e+00;

You can also initialize real variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:

This example initializes the variable sum to 5.0, which
has been declared as
static single.

procedure foo (in x : single;
out y: single);

 var
sum: static single := 5.0;

The example in the following section defines six valid real constants, two of which do not have a digit after the decimal point.

real Constants

Here is an example that of a real constant:

const
	n = 42.57;
	n2 = 4E12;
	n3 = 567.;
	n4 = 83.;
	n5 = cos(567.)/2;
		n6 = succ(sqrt(5+4));

Data Representation

Pascal represents real, single, shortreal, double, and longreal data types according to the IEEE standard, A Standard for Binary Floating-Point Arithmetic. Figure 2-1 shows the representation of a 32-bit floating point number; Figure 2-2 shows the representation of a 64-bit floating point number.

Figure  2-1 32-Bit Floating-Point Number

Figure  2-2 64-Bit Floating-Point Number

A real number is represented by this form:

	(-1)sign * 2 exponent-bias *1.f
f is the bits in the fraction. Extreme exponents are represented as shown in Table 2-2.




Table  2-2 Representation of Extreme Exponents

Exponent
Description

zero (signed)

Represented by an exponent of zero and a fraction of zero.

Subnormal number

Represented by (-1) sign * 2 1-bias *0.f, where f is the bits in the significand.

Not a Number (NaN)

Represented by the largest value that the exponent an assume (all ones), and a nonzero fraction.

Normalized real numbers have an implicit leading bit that provides one more bit of precision than usual.

Table 2-3 shows the hexadecimal representation of several numbers.




Table  2-3 Hexadecimal Representation of Selected Numbers

Value
32-bit Floating-Point Number
64-bit Floating-Point Number

+0

00000000

0000000000000000

-0

80000000

8000000000000000

+1.0

3F800000

3FF0000000000000

-1.0

BF800000

BFF0000000000000

+2.0

40000000

4000000000000000

+3.0

40400000

4008000000000000

+Infinity

7F800000

7FF0000000000000

-Infinity

FF800000

FFF0000000000000

NaN

7Fxxxxxx

7FFxxxxxxxxxxxxx


Integer

Pascal supports the standard predeclared integer data type. As extensions to the standard, Pascal also supports the integer16 and integer32 data types, integer initialization in the variable declaration, and integer constants in a base other than base 10.

Integer Variables

Table 2-4 lists the minimum and maximum values of the integer data types.




Table  2-4 Integer Data Types

Type
Number of Bits
Maximum Value
Minimum Value

integer (without -xl option)

32

2,147,483,647

-2,147,483,648

integer (with
-xl option)

16

32,767

-32,768

integer16

16

32,767

-32,768

integer32

32

2,147,483,647

-2,147,483,648

This example declares three integer variables:

var
	i: integer;
	score: integer16;
	number: integer32;

To define an unsigned integer in Pascal, use a subrange declaration. The subrange syntax indicates the lower and upper limits of the data type, as follows:

This code limits the legal values for the variable unsigned_int to 0 through 65536.

type
 	unsigned_int = 0..65536;
var
 	u: unsigned_int;

Integer Initialization

To initialize integer variables when you declare them in the var declaration part of your program, put an assignment statement in the declaration, as follows:

This example initializes the variables a and b to 50 and c to 10000.

 var		a, b: integer32 := 50;
 		c: integer16 := 10000;

You can also initialize integer variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:

This code initializes the variable sum to 50, which has been declared as static integer16.

procedure show (in x : integer16;
 		out y: integer16);
 var
	sum: static integer16 := 50;

Integer Constants

You define integer constants in Pascal the same as you do as in standard Pascal.

Here is an example:

const
	x = 10;
	y = 15;
	n1 = sqr(x);
	n2 = trunc((x+y)/2);
	n3 = arshft(8, 1);

maxint and minint

The value Pascal assigns to the integer constants maxint and minint is shown in Table 2-5.

Table  2-5 Values for maxint and minint-


Without -xl
With -xl
Constant
Bits
Value
Bits
Value

maxint

32

2,147,483,647

16

32,767

minint

32

-2,147,483,648

16

-32,768

In Another Base

To specify an integer constant in another base, use the following format:

base#number
base is an integer from 2 to 36. number is a value in that base. To express number, use the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and then use the letters a to z. Case is insignificant; a is equivalent to A.

You can optionally put a positive sign (+) or negative sign (-) before base. The sign applies to the entire number, not the base.

This code specifies integers in binary, octal, and hexadecimal notation.

power := 2#10111;             (* binary (base 2) *) 
fraction_of_c := -8#76543;    (* octal (base 8) *)
percentage := +16#fd9c;       (* hexadecimal (base 16) *)

Data Representation

Pascal represents integer, integer16, and integer32 data types in twos complement format. Figure 2-3 shows the representation of a 16-bit integer. Similarly, Figure 2-4 shows the representation of a 32-bit integer.

Figure  2-3 16-Bit Integer

Figure  2-4 32-Bit Integer


boolean

Pascal supports the standard predeclared data type boolean. As an extension to the standard, Pascal permits you to initialize boolean variables in the variable declaration.

boolean Variables

In Pascal, you declare boolean variables the same as in standard Pascal. Both of the following are valid boolean variables:

This example declares the variables cloudy and sunny as boolean.

 var
	cloudy: boolean;
	sunny: boolean;

boolean Initialization

To initialize a boolean variable when you declare it in the var declaration of your program, use an assignment statement, as follows:

This example initializes cloudy to true and sunny to false.

var
	cloudy: boolean := true;
	sunny: boolean := false;

You can also initialize boolean variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:

This code initializes the variable rainy to false, which has been declared as static boolean.

function weather (x: integer): boolean;

var 	
	rainy: static boolean := false;

boolean Constants

You declare boolean constants in Pascal the same as in standard Pascal. Three valid boolean constants follow:

This example declares the constants a as true and b as false. It also declares n as the value odd(y).

const 
	a = true; 	
	b = false; 
	y = 15; 
	n = odd(y);

Data Representation

Pascal allocates one byte for each boolean variable. Figure 2-5 shows how Pascal internally represents a true boolean variable; Figure 2-6 shows how Pascal represents a false boolean variable.

Figure  2-5 true boolean Variable

Figure  2-6 false boolean Variable


Character

Pascal supports the standard predeclared data type char. As extensions to the standard, Pascal supports character initialization in the variable declaration and four nonstandard character constants.

Character Variables

You declare character variables in Pascal the same as you do in standard Pascal. Each of the following is a valid character variable:

var
	current_character: char;
	largest: char;
	smallest: char;

Character Initialization

To initialize a character variable when you declare it in the var declaration of your program, create an assignment statement, as follows:

This example initializes the variable pass to A and fail to F.

var
	pass: char := 'A';
	fail: char := 'F';

You can also initialize character variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:

This example initializes the variable grade1 to A, grade2 to B, and grade3 to C. All three variables are declared as static char.

procedure grades;
var
	grade1: static char := 'A';
	grade2: static char := 'B';
	grade3: static char := 'C';

Character Constants

Pascal extends the standard definition of character constants by predeclaring the four character constants in Table 2-6.




Table  2-6 Nonstandard Predeclared Character Constants

Constant
Description

minchar

Equal to char(0)

maxchar

Equal to char(255)

bell

Equal to char(7) (which makes your terminal beep)

tab

Equal to char(9) (which makes a tab character)

Data Representation

Pascal allocates one byte for each character variable.


Enumerated Types

Pascal supports enumerated data types with extensions that allow you to input enumerated types with the read and readln procedures and output them with the write and writeln procedures. See the listings on read and write in Chapter 7, "Input and Output," for details.

Enumerated Variables

You declare enumerated data types in Pascal the same as in standard Pascal.

type
	continents =(North_America, South_America,
	             Asia, Europe, Africa, Australia,
	             Antartica); 
	gem_cuts = (marquis, emerald, round, pear_shaped);

var 
	x: gem_cuts;
	index: continents;

Data Representation

When you compile your program without the -xl option, Pascal represents enumerated types as either 8 or 16 bits, depending on the number of elements defined for that type. With -xl, Pascal represents variables of enumerated type as 16 bits. Pascal stores enumerated types as integers corresponding to their ordinal value.

Figure 2-7 shows the representation of a 16-bit enumerated variable.

Figure  2-7 16-Bit Enumerated Variable
As an example, suppose you defined a group of colors, as follows:

colors = (red, green, blue, orange);
Pascal represents each value as shown in Figure 2-8.

Figure  2-8 Sample Enumerated Representation


Subrange

Pascal supports a subrange of integer, boolean, character, and enumerated data types.

The Pascal subrange type is extended to allow constant expressions in both the lower and upper bound of the subrange. The lower bound expression is restricted by requiring that the expression not begin with a left parenthesis.

Subrange Variables

See "Integer Variables" on page 17 for an example of a subrange declaration.

Data Representation

The Pascal subrange takes up the number of bits required for its minimum and maximum values. Table 2-7 shows the space allocation of six subranges.




Table  2-7 Subrange Data Representation

Minimum/Maximum Range
Without -xl (Bits)
With -xl (Bits)

0..127

8

16

-128..127

8

16

0..255

16

16

-32768..32767

16

16

0...65536

32

32

-2,147,483,648..2,147,483,647

32

32

Figure 2-9 shows how Pascal represents a 16-bit subrange. Similarly, Figure 2-10 shows how Pascal represents a 32-bit subrange.

Figure  2-9 16-Bit Subrange

Figure  2-10 32-Bit Subrange


Record

Pascal supports the standard record and packed record data types. As an extension, Pascal permits you to initialize a record variable when you declare it in the variable declaration.

Record Variables

You declare records in Pascal the same as in standard Pascal, as shown in the following example:

type
	MonthType = (Jan, Feb, Mar, Apr, May, Jun, Jul,
	Aug, Sep, Oct, Nov, Dec);
	DateType = record
		Month : MonthType; 
		Day : 1..31;
		Year : 1900..2000;
	end;

	Appointment = record
		Date : DateType;
		Hour : 0..2400;
	end;

Record Initialization

To initialize a field in a record when you declare it in the var declaration of your program, use either of the following two formats:

 [a := FALSE ,
 b := TRUE ]

[  FALSE  ,
TRUE   ]

You can also initialize record variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static.

The Pascal program, init_rec.p. This example shows a record initialization by name, by position, and by name and position.

program init_rec(output);

{ This program initializes a record. }

type
    enumerated_type = (red, green, blue, orange, white);
    record_type = 
      record 
          c: char;
          st: set of char;
          z: array [1..10] of char;
          case colors: enumerated_type of
              red: ( b: boolean;
                     s: single );
              green: ( i16: integer16;
                       d: double )
      end;
var
    { Initialization by name. }
    rec1: record_type :=
      [st := ['a', 'b', 'c'],
      c := 'A',
      z := 'ARRAY1',
      colors := green,
      i16 := 32767];

Initializing Record Variables (Screen 1 of 2)

    { Initialization by position. }
    rec2: record_type :=
      ['X',
      ['x', 'y', 'z'],
      'ARRAY2',
      red,
      true];
    { Initialization by name and position. }
    rec3: record_type :=
      [colors := red,
      true,
      1.16,
      st := ['m', 'n', 'o'],
      'ARRAY3'];

begin
    writeln('char       ', rec1.c);
    writeln('char array ', rec1.z);
    writeln('integer    ', rec1.i16);
    writeln;
    writeln('char       ', rec2.c);
    writeln('char array ', rec2.z);
    writeln('boolean    ', rec2.b);
    writeln;
    writeln('char array ', rec3.z);
    writeln('boolean    ', rec3.b);
    writeln('single     ', rec3.s)
end. { record_example }

Initializing Record Variables (Screen 2 of 2)

The commands to compile and execute init_rec.p

hostname% pc init_rec.p
hostname% a.out
char       A
char array ARRAY1    
integer         32767

char       X
char array ARRAY2    
boolean    true

char array ARRAY3    
boolean    true
single      1.160000e+00

Data Representation of Unpacked Records

This section describes the data representations of unpacked fixed and variant records.

Fixed Records

Pascal allocates fields in a fixed record so that they assume the natural alignment of the field type. The alignment of a record is equal to the alignment of the largest element. The size of the record is a multiple of the alignment.

Variant Records

The space Pascal allocates for a variant record is the same with or without the -xl option.

Data Representation of Packed Records

Table 2-8, Table 2-9, and Table 2-10 show how Pascal aligns fields in a packed record.


Note - In packed records, bit-aligned fields do not cross word boundaries.

Packed Record Storage Without the -xl Option


Table  2-8 Packed Record Storage Without -xl-

Data Type
Size
Alignment

integer

4 bytes

4 bytes

integer16

2 bytes

Bit-aligned

integer32

4 bytes

4 bytes

real

8 bytes

8 bytes

single

4 bytes

4 bytes

shortreal

4 bytes

4 bytes

double

8 bytes

8 bytes

longreal

8 bytes

8 bytes

boolean

1 bit

Bit-aligned

char

1 byte

1 byte

enumerated

Number of bits required to represent the highest ordinal value

Bit-aligned

subrange of char

1 byte

1 byte

all other subrange

Number of bits required to represent the highest ordinal value

Bit-aligned

set of cardinality <= 32

One bit per element

Bit-aligned

set of cardinality > 32

Same as if unpacked

4 bytes

array

Requires the same space required by the base type of the array

Same as element type

Packed Record Storage with the -xl Option

Table  2-9 Packed Record Storage with -xl-

Data Type
Size
Alignment

real

4 bytes

4 bytes

integer

2 bytes

Bit-aligned

Packed Record Storage with the -calign Option

Table  2-10 Packed Record Storage with -calign

Data Type
Size
Alignment

integer16

2 bytes

2 bytes

The following example declares a packed record. Table 2-11 shows the alignment and sizes of the fields of the record. Figure 2-11 shows the representation of this record.

type 
	small = 0..128; 
	medium = 0..255; 
	large = 0..65535; 
	colors = (green, blue, orange, white, black, magenta, gray); 
	sets = (autumn, summer, winter, fall); 
	vrec1 = packed record 
		a: integer16; 
		b: boolean; 
		e: colors; 
		sm: small; 
		med: medium; 
		lg: large; 
		se: sets; 
		x: integer32; 
	end;


Table  2-11 Sample Sizes and Alignment of Packed Record

Field
Size (Bits)
Alignment

a

16

16 bit-aligned

b

1

Bit-aligned

e

3

Bit-aligned

sm

8

Bit-aligned

med

16

Bit-aligned

lg (without -xl)

32

32 bit-aligned

lg (with -xl)

16

16 bit-aligned

se

4

Bit-aligned

x

32

32 bit-aligned

Figure  2-11 Sample Packed Record (Without -xl)


Array

Pascal supports the standard array data type. As extensions to the standard, Pascal supplies the predeclared character array types alfa, string, and varying and permits you to initialize an array variable when you declare it in the variable declaration.

Array Variables

In addition to the standard array data types, this compiler supports the three data types in Table 2-12, which include a variable-length string.




Table  2-12 Array Data Types

Type
Description

alfa

An array of char 10 characters long.

string

An array of char 80 characters long.

varying

A string of variable length. You declare a varying string as follows:

varying[upper_bound] of char; upper_bound is an integer between 0 and 65,535

You can assign a variable-length string a string of any length, up to the maximum length you specify in the declaration. Pascal ignores any characters you specify over the maximum. It does not pad the unassigned elements with spaces if you specify a string under the maximum. When you output a variable-length string with write or writeln, the procedure writes only the characters included in the string's current length.

You also can assign a variable-length string to a fixed-length string. If the variable-length string is shorter than the fixed-length string, the fixed-length string is padded with blanks. If the variable-length string is longer than the fixed-length string, the string is truncated.

The following program demonstrates the differences between the fixed-length and varying data types:

The Pascal program, varying.p

program varying_example(output);

{ This program demonstrates the differences
  between fixed- and variable-length strings. }

var
	name1: array [1..25] of char;    { String of size 25. }
	name2: array [76..100] of char;  { String of size 25. }
	name3: alfa;                     { String of size 10. }
	name4: string;                   { String of size 80. }
	name5: varying [25] of char;     { Varying string. }
	name6: varying [25] of char;     { Varying string. }

begin
	name1 := 'van Gogh';
	name2 := 'Monet';
	name3 := 'Rembrandt';
	name4 := 'Breughel';
	name5 := 'Matisse';
 	name6 := 'Cezanne';
	writeln(name1, ' and ', name2, '.');
	writeln(name3, ' and ', name4, '.');
	writeln(name5, ' and ', name6, '.')
end. { varying_example }

The commands to compile and execute varying.p

hostname% pc varying.p
hostname% a.out
van Gogh                  and Monet                    .
Rembrandt  and 
Breughel                                                                        .
Matisse and Cezanne.

Array Initialization

To initialize an array variable when you declare it in the var declaration of your main program, use an assignment statement after the declaration. Pascal offers you the following four different formats:

This code initializes the first five elements of int to maxint,
1, -32767, 5, and 20. The first six elements of c1 are assigned the characters 1 through 6. Because c1 is a fixed-length string, the last four characters are padded with blanks.

var
	int : array[1..10] of integer := [maxint, 1, -327, 5, 20];
	c1 :  array[1..10] of char := '123456';

In this example, the compiler assigns the upper bound of 5 to int and of 6 to c1.

var
	i : integer;
	int : array[1..*] of integer := [maxint, 1, -32767, 5, 20];
	c1 : array[1..*] of char := '123456';

This code initializes all the first 50 values of int2 to 1 and the second 50 values to 2.

var
	int2 : array[1..100] of integer := [50 of 1, 50 of 2];

This example initializes all 100 elements of int4 to 327. The example also initializes the multidimensional array int5 to an array of 10 rows and columns. The compiler initializes all 10 elements in the first row to 327, the first three elements of the second row to 8, and all 10 elements of the third row to 88.

var
	int4 : array[1..100] of integer := [* of 327];
	int5 : array[1..10,1..10] of integer := [
[* of 327],
[3 of 8],
[10 of 88],
];

When you initialize an array in the var declaration, the compiler sets those elements for which it doesn't find data to zero.

You can also initialize array variables in the var declaration of a procedure or function; however, you must also declare the variable as static.

Packed Arrays

Although you can define an array as packed, it has no effect on how the Pascal compiler allocates the array data space.

Data Representation

The elements of an array require the same space as that required by the base type of the array. However, there are two exceptions to this. With the
-calign option, the size of all arrays is the size of each element times the number of elements. When you use the -calign and -xl options together, arrays are the same as with -calign alone, except the size of an array of booleans is always a multiple of two.


Set

Pascal supports sets of elements of integer, boolean, character, and enumerated data types. As extensions to the standard, Pascal predefines a set of intset; you can then initialize a set variable when you declare it in the var declaration of your program.

Set Variables

In Pascal, you declare set variables the same as you do in standard Pascal. The following is a valid set variable:

 type
	character_set = set of char;

var
	letters: character_set;

Pascal predefines the set intset as the set of [0..127].

Set Initialization

To initialize a set variable when you declare it in the var declaration of your program, create an assignment statement, as follows:

This code initializes citrus
to the set of orange, lemon,
and lime.

type
	fruit = (orange, lemon, lime, apple, banana);
var 
	citrus: set of fruit := [orange, lemon, lime];

You can also initialize set variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:

This example initializes primary to the set of red, yellow, and blue. It also initializes grays to the set of white and black.

procedure assign_colors;
type
	colors = (white, beige, black, red, blue,
yellow, green);
var
	primary: static set of colors := [red, yellow,
blue];
	grays: static set of colors := [white, black];

Packed Sets

Although you can define a set as packed, it has no effect on how the compiler allocates the set data space.

Data Representation

Pascal implements sets as bit vectors, with one bit representing each element of a set. The maximum ordinal value of a set element is 32,768.

The size of a set is determined by the size of the ordinal value of maximal element of the set plus one. Sets are allocated in multiples of 16 bits; therefore, the smallest set has size 16 bits. The ordinal value of the minimal element must be equal to or greater than 0. Sets have an alignment of four bytes when they are longer than 16 bits; otherwise their alignment is two bytes. For example, 'set of 1..20' has a four-byte alignment and 'set of 1..15' has a two-byte alignment.

With the -calign option, sets have an alignment of two bytes. The size is the same as the default.

Table 2-13 shows the data representation of four sets.




Table  2-13 Data Representation of Sets

Set
Description

set of 0..15

This set requires 16 bits because 15 is the maximal element, and
15 + 1 = 16.

set of 0..16

This set requires 32 bits because 16 is the maximal element. 16 + 1 = 17, and the next multiple of 16 above 17 is 32.

set of 14..15

This set requires 16 bits because 15 is the element, and 15 + 1 = 16.

set of char

This set requires 256 bits because the range of char is chr(0)..chr(255). The ordinal value of the maximal element is 255, and 255+1 = 256, which is divisible by 16.

You can visualize the bit vector representation of a set as an array of bits starting from the highest element to the lowest element. For example, the representation of the following set is shown in Figure 2-12.

   var
		smallset: set of 2..15 := [7,4,3,2];

Figure  2-12 Small Set
The representation of this larger set is shown in Figure 2-13.

var
	largeset: set of 2..255 := [7,4,3,2];

Figure  2-13 Large Set


File

Pascal treats files declared as file of char the same as files declared as text, except when you use the -s -s0, -s1, -V0, or -V1 options. In this case, the compiler treats the two data types differently, as required by standard Pascal.


Pointer

Pascal supports the standard Pascal pointer and the nonstandard universal pointer and procedure and function pointer.

Standard Pointer

The standard pointer is the same in Pascal and standard Pascal.

Universal Pointer

The universal pointer data type, univ_ptr, is compatible with any pointer type. Use univ_ptr to compare a pointer of one type to another, to assign a pointer of one type to another, or to weaken type checking when passing parameters of pointer types.

When the type of a formal parameter is univ_ptr, the type of the corresponding actual parameter can be of any pointer type, or vice versa.

You cannot dereference a univ_ptr variable: you cannot find the contents of the address to which univ_ptr points.

The Pascal program, univ_ptr.p, which prints the value of the floating-point variable r in hexadecimal format.

program univ_ptr_example;

{ This program demonstrates how to use
  universal pointers. }

var
    i: integer32;
    r: single;
    ip: ^ integer32;
    rp: ^ single := addr(r);
    up: univ_ptr;

begin
    r := 10.0;
    { The next two statements are equivalent to rp := ip.
      However, rp := ip is not legal since they are
      different types. }
    up := rp;
    ip := up;
    writeln(ip^ hex);
    { This will do the same thing but uses transfer functions. }
    writeln(integer32(r) hex)
end. { univ_ptr_example }

The commands to compile and execute univ_ptr.p

hostname% pc univ_ptr.p
hostname% a.out
41200000
41200000

Procedure and Function Pointers

The following is an example that shows how to use procedure and function pointers in Pascal.

The Pascal program, pointer.p, which demonstrates how to print out enumerated values using procedure pointers.

program pointer_example;

type
    colors = (red, white, blue);
    procptr = ^ procedure; { Procedure pointer type. }

procedure printred;

begin
    writeln('RED')
end; { printred }

procedure printwhite;

begin
    writeln('WHITE')
end; { printwhite }

procedure printblue;

begin
    writeln('BLUE')
end; { printblue }

var
    { Array of procedure pointers. }
    colorprinter: array [colors] of procptr :=
              [addr(printred),
              addr(printwhite),
              addr(printblue)];
    c: colors;
    desc_proc: procptr;

begin
    write('Enter red, white, or blue:  ');
    readln(c);
    desc_proc := colorprinter[c];
    desc_proc^
end. { pointer_example }

The commands to compile and execute pointer.p

hostname% pc pointer.p
hostname% a.out
Enter red, white, or blue:  red
RED

Pointer Initialization

To initialize a pointer variable when you declare it in the var declaration of your program, use an assignment statement, as follows:

This example initializes the variable rp to addr(r). Legal values for compile-time initializations are NIL, addr(0)
of variables, procedures, strings, and set constants, and
previously declared constants of the same pointer type.

 var 
	rp : ^single := addr(r);
	pp : ^procedure := NIL;
	sp : ^string := addr('Title');

You can also initialize pointer variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static.

Data Representation

Pascal represents a pointer as shown in Figure 2-14.

Figure  2-14 Pointer

Previous Next Contents Index Doc Set Home