Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 5/1-dimensional arrays: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
115C - One-dimensional Arrays (author: Tao Yue, state: unchanged)
22
33Suppose you wanted to read in 5000 integers and do something with them. How would you store the integers?
44
55You could use 5000 variables, lapsing into:
66
7-`aa, ab, ac, ad, ... aaa, aab, ... aba, ...`
7+```
8+aa, ab, ac, ad, ... aaa, aab, ... aba, ...
9+```
810
911But this would grow tedious (after declaring those variables, you have to read values into each of those variables).
1012
1113An array contains several storage spaces, all the same type. You refer to each storage space with the array name and with a subscript. The type definition is:
1214
1315``` pascal
1416type
1517 typename = array [enumerated_type] of another_data_type;
1618```
1719
1820The data type can be anything, even another array. Any enumerated type will do. You can specify the enumerated type inside the brackets, or use a predefined enumerated type. In other words,
1921
2022``` pascal
2123type
2224 enum_type = 1..50;
2325 arraytype = array [enum_type] of integer;
2426```
2527
2628is equivalent to
2729
2830``` pascal
2931type
3032 arraytype = array [1..50] of integer;
3133```
3234
3335Aside: This is how strings are actually managed internally — as arrays. Back before modern Pascal compilers added native support for strings, programmer had to handle it themselves, by declaring:
3436
3537``` pascal
3638type
3739 String = packed array [0..255] of char;
3840```
3941
4042and using some kind of terminating character to signify the end of the string. Most of the time it's the null-character (ordinal number 0, or ord(0)). The packed specifier means that the array will be squeezed to take up the smallest amount of memory.
4143
4244Arrays of characters representing strings are often referred to as buffers, and errors in handling them in the C or C++ programming languages may lead to buffer overruns. A buffer overrun occurs when you try to put, say, a 200-character string into a 150-length array. If memory beyond the buffer is overwritten, and if that memory originally contained executable code, then the attacker has just managed to inject arbitrary code into your system. This is what caused the famous Slammer worm that ran rampant on the Internet for several days. Try it in Pascal and see what happens.
4345
4446Arrays are useful if you want to store large quantities of data for later use in the program. They work especially well with for loops, because the index can be used as the subscript. To read in 50 numbers, assuming the following definitions:
4547
4648``` pascal
4749type
4850 arraytype = array[1..50] of integer;
4951
5052var
5153 myarray : arraytype;
5254```
5355
5456use:
5557
5658``` pascal
5759for count := 1 to 50 do
5860 read (myarray[count]);
5961```
6062
6163Brackets `[ ]` enclose the subscript when referring to arrays.
6264
6365``` pascal
6466myarray[5] := 6;
6567```
6668
6769---
6870*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 5/1-dimensional arrays", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history