Sign in Join
Wiki copytemporary

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

Revision 2 to 3. 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-```
7+```text
88aa, ab, ac, ad, ... aaa, aab, ... aba, ...
99```
1010
1111But this would grow tedious (after declaring those variables, you have to read values into each of those variables).
1212
1313An 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:
1414
1515``` pascal
1616type
1717 typename = array [enumerated_type] of another_data_type;
1818```
1919
2020The 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,
2121
2222``` pascal
2323type
2424 enum_type = 1..50;
2525 arraytype = array [enum_type] of integer;
2626```
2727
2828is equivalent to
2929
3030``` pascal
3131type
3232 arraytype = array [1..50] of integer;
3333```
3434
3535Aside: 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:
3636
3737``` pascal
3838type
3939 String = packed array [0..255] of char;
4040```
4141
4242and 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.
4343
4444Arrays 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.
4545
4646Arrays 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:
4747
4848``` pascal
4949type
5050 arraytype = array[1..50] of integer;
5151
5252var
5353 myarray : arraytype;
5454```
5555
5656use:
5757
5858``` pascal
5959for count := 1 to 50 do
6060 read (myarray[count]);
6161```
6262
6363Brackets `[ ]` enclose the subscript when referring to arrays.
6464
6565``` pascal
6666myarray[5] := 6;
6767```
6868
6969---
7070*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