1 / 6

Declaring an array without a type statement

Learn how to declare and initialize arrays in Turbo Pascal, including examples of legal arrays and initializing techniques.

Télécharger la présentation

Declaring an array without a type statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. Arrays and the watch window

  4. 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.

  5. 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.

  6. 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’;

More Related