Introduction to Arrays
An array in Pascal is a fixed-size, ordered collection of elements that all share the same type, declared with a syntax such as 'var scores: array[1..10] of Integer;' where the index range and element type are both fixed at compile time. Because the size is baked into the type, Pascal can check bounds and allocate the exact memory needed up front, which is very different from dynamically resizable structures in other languages.
Cricket analogy: A scorecard array is like a fixed 11-player batting lineup for a Test match — you declare 11 slots before the toss, and Virat Kohli's score lives at exactly one fixed position you can't renumber mid-innings.
Declaring and Indexing Arrays
Pascal array indices are not restricted to integers starting at zero; the index type can be any ordinal type, including subranges like 1..10, characters like 'A'..'Z', or enumerated types, and you typically declare a reusable index range with a TYPE statement such as 'type TDayRange = 1..7;' before using it in an array declaration. Accessing an element uses square brackets, e.g. 'scores[3] := 87;', and the compiler (in range-checked builds) raises a runtime error if you index outside the declared bounds.
Cricket analogy: Indexing by an enumerated type is like addressing fielding positions with names (slip, gully, midOn) instead of numbers — Pascal lets you write array[TFieldPos] of Player just as a captain calls out named positions rather than seat numbers.
Multidimensional Arrays
Pascal supports multidimensional arrays directly, such as 'var grid: array[1..3, 1..3] of Real;' for a 3x3 matrix, and you can index elements either as 'grid[2,3]' or the equivalent nested form 'grid[2][3]' depending on how the type was declared, since a two-dimensional array is really an array of arrays under the hood. This makes Pascal well suited to representing matrices, game boards, and tables without needing external libraries.
Cricket analogy: A 3x3 grid mirrors a Duckworth-Lewis-style table with rows for overs remaining and columns for wickets lost, where grid[overs, wickets] looks up one resource-percentage value directly.
program ArrayDemo;
type
TScoreArray = array[1..5] of Integer;
var
scores: TScoreArray;
i, total: Integer;
begin
scores[1] := 45; scores[2] := 78; scores[3] := 90;
scores[4] := 62; scores[5] := 88;
total := 0;
for i := 1 to 5 do
total := total + scores[i];
writeln('Average: ', total / 5:0:2);
end.Accessing an array element outside its declared bounds, such as scores[6] on an array[1..5], is undefined or a runtime range-check error depending on compiler settings ({$R+}); always validate loop bounds against Low(scores) and High(scores) rather than hardcoding numbers.
- Pascal arrays have a fixed size and type determined entirely at declaration time.
- Index types can be any ordinal type: integer subranges, characters, or enumerations.
- Use 'type' declarations to create reusable, named array types like TScoreArray.
- Multidimensional arrays like array[1..3,1..3] of Real model matrices and tables directly.
- Low() and High() return the declared bounds and should be used instead of hardcoded numbers.
- Range checking ({$R+}) catches out-of-bounds access at runtime and should be enabled during development.
- Arrays can be passed to procedures by value (copied) or by reference using 'var' parameters.
Practice what you learned
1. What does the declaration 'var scores: array[1..10] of Integer;' establish?
2. Which of the following is a valid index type for a Pascal array?
3. How would you declare a 3x3 matrix of real numbers in Pascal?
4. What is the purpose of the {$R+} compiler directive?
Was this page helpful?
You May Also Like
Records in Pascal
Understand how Pascal records bundle fields of different types into a single structured entity, including variant records.
Strings in Pascal
Learn how Pascal's length-prefixed strings work, from fixed-length capped strings to dynamic AnsiString, plus core string functions.
Sets in Pascal
Explore Pascal's native set type — union, intersection, difference, and membership testing over ordinal values.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics