0% found this document useful (0 votes)
9 views

Ex 05

Uploaded by

skamelrech2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Ex 05

Uploaded by

skamelrech2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

118 - 04: Procedures and Functions

Marco Cantù, Object Pascal Handbook


05: Arrays and Records - 119

05: arrays and


records

When I introduced data types in Chapter 2, I referred to the fact that in Object Pas-
cal there are both built in data types and type constructors. A simple example of a
type constructor is the enumerated type, covered in that chapter.
The real power of type definition comes with more advanced mechanisms, such as
arrays, records, and classes. In this chapter I'll cover the first two, which in their
essence date back to the early definition of Pascal, but have been changed so much
over the years (and made so powerful) that they barely resemble their ancestral type
constructors with the same name.
Towards the end of the chapter I'll also briefly introduce some advanced Object Pas-
cal data types as pointers. The real power of custom data types, however, will be
unveiled in Chapter 7, where we'll start looking into classes and object-oriented pro-
gramming.

Marco Cantù, Object Pascal Handbook


120 - 05: Arrays and Records

Array Data Types


Array types define lists with elements of a specific type. These lists can have a fixed
number of elements (static arrays) or of a variable number of elements (dynamic
arrays). You generally use an index within square brackets to access one of the ele-
ments of an array. Square brackets are also used to specify the number of values of a
fixed size array.
The Object Pascal language supports different array types, from traditional static
arrays to dynamic ones. Use of dynamic arrays is recommended, particularly with
the mobile versions of the compiler. I'll introduce static arrays first, and later focus
on dynamic ones.

Static Arrays
Traditional Pascal language arrays are defined with a static or fixed size. An example
is in the following code snippets, which defines a list of 24 integers, presenting the
temperatures during the 24 hours of a day:
type
TDayTemperatures = array [1..24] of Integer;
In this classic array definition, you can use a subrange type within square brackets,
actually defining a new specific subrange type using two constants of an ordinal
type. This subrange indicates the valid indexes of the array. Since you specify both
the upper and the lower index of the array, the indexes don’t need to be zero-based,
as it is the case in C, C++, Java, and most other languages (although 0-based arrays
are also quite common in Object Pascal). Notice also that static array indexes in
Object Pascal can be numbers, but also other ordinal types like characters, enumer-
ated types, and more. Non-integral indexes are quite rare, though.

note There are languages like JavaScript that make heavy use of associative arrays. Object Pascal
arrays are limited to ordinal indexes, so you cannot directly use a string as index. There are ready
to use data structures in the RTL implementing Dictionaries and other similar data structures that
provide such features. I'll cover them in the chapter about Generics, in the third part of the book.

Since the array indexes are based on subranges, the compiler can check their range.
An invalid constant subrange results in a compile-time error; and an out-of-range
index used at run-time results in a run-time error, but only if the corresponding
compiler option is enabled.

Marco Cantù, Object Pascal Handbook


05: Arrays and Records - 121

note This is the Range checking option of the Runtime errors group of the Compiling page of the
Project Options dialog of the IDE. I've already mentioned this option in Chapter 2, in the section
“Subrange Types”.

Using the array definition above, you can set the value of a DayTemp1 variable of the
TDayTemperatures type as follows (and as I've done in the ArraysTest application
project, from which the following code snippets have been extracted):
type
TDayTemperatures = array [1..24] of Integer;

var
DayTemp1: TDayTemperatures;

begin
DayTemp1 [1] := 54;
DayTemp1 [2] := 52;
...
DayTemp1 [24] := 66;

// The following line causes:


// E1012 Constant expression violates subrange bounds
// DayTemp1 [25] := 67;
Now a standard way to operate on arrays, given their nature, is to use for cycles. This
is an example of a loop used to display all of the temperatures for a day:
var
I: Integer;
begin
for I := 1 to 24 do
Show (I.ToString + ': ' + DayTemp1[I].ToString);
While this code works, having hard-coded the array boundaries (1 and 24) is far
from ideal, as the array definition itself might change over time and you might want
to move to using a dynamic array.

Array Size and Boundaries


When you work with an array, you can always test its boundaries by using the stan-
dard Low and High functions, which return the lower and upper bounds. Using Low
and High when operating on an array is highly recommended, especially in loops,
since it makes the code independent of the current range of the array (which might
go from 0 to the length of the array minus one, might start form 1 and reach the
array's length, or have any other subrange definition). If you should later change the
declared range of the array indexes, code that uses Low and High will still work. If
you write a loop hard-coding the range of an array you’ll have to update the code of

Marco Cantù, Object Pascal Handbook

You might also like