60 likes | 69 Views
Learn how to declare and initialize arrays in Turbo Pascal, including examples of legal arrays and initializing techniques.
E N D
Declaring an array without a type statement • You can declare an array in the VAR section:VAR MyArray: ARRAY[1..10] OF Char; • If you do so, you do not define a reusable type, but only the dimensions of a particular variable. In other words you cannot now declare 3 or 4 variables of type MyArray.
Examples of legal arrays • An array of integers with character subscripts:TYPE CharArray = ARRAY[‘a’..’z’] OF integer; • An array of characters with integer subscripts:TYPE IntArray = ARRAY[-10..10] OF char: • An array of reals with Boolean subscripts:TYPE RealArray = ARRAY[false..true] OF real; • reals can not be subscripts because they are not an ordinal type.
Initializing arrays • In order to initialize an array, you must use a loop (or explicitly initialize each element of the array)FOR Index := 1 TO 10 DO MyArray[Index] := 0; • Note: In the case of integers, Turbo Pascal would automatically initialize the values to 0 but other Pascal compilers may not.
an array of chars vs. a string • An array of characters is very similar to a string. The biggest difference is the first byte of a string contains the length of that string (remember I said the maximum length is 255 characters) • You can access components of a string the way you would an array.
Typed CONST • Typed constants can be changed. For example, with the following definition:CONST Days: ARRAY [1..7] OF string = (‘Sun’, ‘Mon’, ‘Tues’, ‘Wed’, ‘Thurs’, ‘Fri’, ‘Sat’); • You can change ‘Mon’ to ‘Monday’ byDays[2] := ‘Monday’;