Difference between revisions of "Basic Pascal Tutorial/Chapter 5/1-dimensional arrays"

From Lazarus wiki
Jump to navigationJump to search
Line 9: Line 9:
  
 
An 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:
 
An 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:
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  typename <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>array</strong></font> <font color="#000000"><strong>[</strong></font>enumerated_type<font color="#000000"><strong>]</strong></font> <font color="#006699"><strong>of</strong></font> another_data_type<font color="#000000"><strong>;</strong></font>
+
type
 +
  typename = array [enumerated_type] of another_data_type;
 +
</delphi>
  
 
The 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,
 
The 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,
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  enum_type <font color="#000000"><strong>=</strong></font> <font color="#ff0000">1</font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#ff0000">50</font><font color="#000000"><strong>;</strong></font>
+
type
  arraytype <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>array</strong></font> <font color="#000000"><strong>[</strong></font>enum_type<font color="#000000"><strong>]</strong></font> <font color="#006699"><strong>of</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
  enum_type = 1..50;
 +
  arraytype = array [enum_type] of integer;
 +
</delphi>
  
 
is equivalent to
 
is equivalent to
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  arraytype <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>array</strong></font> <font color="#000000"><strong>[</strong></font><font color="#ff0000">1</font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#ff0000">50</font><font color="#000000"><strong>]</strong></font> <font color="#006699"><strong>of</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
type
 +
  arraytype = array [1..50] of integer;
 +
</delphi>
  
 
Aside: 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:
 
Aside: 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:
  <font color="#006699"><strong>type</strong></font>
+
<delphi>
    <font color="#006699"><strong>String</strong></font> <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>packed</strong></font> <font color="#006699"><strong>array</strong></font> <font color="#000000"><strong>[</strong></font><font color="#ff0000">0</font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#ff0000">255</font><font color="#000000"><strong>]</strong></font> <font color="#006699"><strong>of</strong></font> <font color="#0099ff"><strong>char</strong></font><font color="#000000"><strong>;</strong></font>
+
type
 +
  String = packed array [0..255] of char;
 +
</delphi>
  
 
and 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.
 
and 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.
Line 30: Line 38:
  
 
Arrays 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:
 
Arrays 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:
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  arraytype <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>array</strong></font><font color="#000000"><strong>[</strong></font><font color="#ff0000">1</font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#ff0000">50</font><font color="#000000"><strong>]</strong></font> <font color="#006699"><strong>of</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
type
 +
  arraytype = array[1..50] of integer;
 +
 
 +
var
 +
  myarray : arraytype;
 +
</delphi>
 
   
 
   
  <font color="#006699"><strong>var</strong></font>
 
    myarray <font color="#000000"><strong>:</strong></font> arraytype<font color="#000000"><strong>;</strong></font>
 
 
use:
 
use:
<font color="#006699"><strong>for</strong></font> count <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">1</font> <font color="#006699"><strong>to</strong></font> <font color="#ff0000">50</font> <font color="#006699"><strong>do</strong></font>
+
<delphi>
  <font color="#009966"><strong>read</strong></font> <font color="#000000"><strong>(</strong></font>myarray<font color="#000000"><strong>[</strong></font>count<font color="#000000"><strong>]</strong></font><font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
for count := 1 to 50 do
 +
  read (myarray[count]);
 +
</delphi>
  
 
Brackets <tt>[ ]</tt> enclose the subscript when referring to arrays.
 
Brackets <tt>[ ]</tt> enclose the subscript when referring to arrays.
myarray<font color="#000000"><strong>[</strong></font><font color="#ff0000">5</font><font color="#000000"><strong>]</strong></font> <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">6</font><font color="#000000"><strong>;</strong></font>
+
<delphi>
 +
myarray[5] := 6;
 +
</delphi>
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"

Revision as of 17:04, 5 January 2010

5C - One-dimensional Arrays (author: Tao Yue, state: unchanged)

Suppose you wanted to read in 5000 integers and do something with them. How would you store the integers?

You could use 5000 variables, lapsing into:

aa, ab, ac, ad, ... aaa, aab, ... aba, ...

But this would grow tedious (after declaring those variables, you have to read values into each of those variables).

An 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: <delphi> type

 typename = array [enumerated_type] of another_data_type;

</delphi>

The 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, <delphi> type

 enum_type = 1..50;
 arraytype = array [enum_type] of integer;

</delphi>

is equivalent to <delphi> type

 arraytype = array [1..50] of integer;

</delphi>

Aside: 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: <delphi> type

 String = packed array [0..255] of char;

</delphi>

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

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

Arrays 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: <delphi> type

 arraytype = array[1..50] of integer;
 

var

 myarray : arraytype;

</delphi>

use: <delphi> for count := 1 to 50 do

 read (myarray[count]);

</delphi>

Brackets [ ] enclose the subscript when referring to arrays. <delphi> myarray[5] := 6; </delphi>

previous contents next