Difference between revisions of "Array"

From Free Pascal wiki
Jump to navigationJump to search
(cleanup)
Line 1: Line 1:
An '''array''' is a type that groups a number of [[Variable|variables]] of the same type (such as an array of [[Char|char]], [[Integer|integer]], [[real]] or any other type including user defined types). Different types of variables cannot be grouped into an array. For this purpose, see [[Record|record]]s.
+
An '''array''' is a type that groups a number of [[Variable|variables]] of the same type. Example are an array of [[Char|char]], [[Integer|integer]], [[real]] or any other type including user defined types.
<p> The declaration works as for simple types adding number & basic type.</p>
 
  
The simplest way is as follows:
+
Different types of variables cannot be grouped into an array. For this purpose, see [[Record|record]]s.
 +
 
 +
 
 +
The declaration works similar to that for simple types, but you need to add the number of elements & basic type.
 +
 
 +
The simplest way is as follows - it defines a so-called static array:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 13: Line 17:
 
</syntaxhighlight>   
 
</syntaxhighlight>   
  
simple example:
+
Simple example:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 20: Line 24:
 
   
 
   
 
var
 
var
   Nmbers: simple_integer_array;
+
   Numbers: simple_integer_array;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
complex example:
+
More complex example:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 33: Line 37:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Arrays reflect the mathematics concept of [[vector]]s (one-dimensional array) and matrices (two-dimensional array).
+
Arrays reflect the mathematical concept of  
[[Multidimensional arrays|multidimensional array]] are supported such as [x..y,z..t] and so on.  
+
* [[vector]]s (one-dimensional array) and  
To call a variable you have to put the name of the array and the position <nowiki>a[1..3]</nowiki> and you can use it as a simple variable, but if you want to use parameters you MUST use a structure because else it will cause errors or bugs... (I do not understand, what is meant here).
+
* matrices (two-dimensional array)
 +
[[Multidimensional arrays|multidimensional array]] are supported such as [x..y,z..t] and so on.  
 +
 
 +
To call a variable you have to put the name of the array and the position <code>a[1..3]</code> and you can use it as a simple variable, but if you want to use parameters you MUST use a structure because else it will cause errors or bugs... (I do not understand, what is meant here).
  
 
If it is not possible to know the number of elements at the time of the program compilation, the [[DYNAMIC_ARRAY|dynamic array]] type can be used.
 
If it is not possible to know the number of elements at the time of the program compilation, the [[DYNAMIC_ARRAY|dynamic array]] type can be used.
  
 
{{Data types}}
 
{{Data types}}
 +
[[Category:FPC]]

Revision as of 16:23, 28 December 2013

An array is a type that groups a number of variables of the same type. Example are an array of char, integer, real or any other type including user defined types.

Different types of variables cannot be grouped into an array. For this purpose, see records.


The declaration works similar to that for simple types, but you need to add the number of elements & basic type.

The simplest way is as follows - it defines a so-called static array:

program
...
var 
  variablename: array [startindex..endindex] of type;
begin
  ...

Simple example:

type
  simple_integer_array = array [1..10] of integer;
 
var
  Numbers: simple_integer_array;

More complex example:

type
  more_complex_array = array [0..5,1..3] of extended;
 
var
  specialmatrix: more_complex_array;

Arrays reflect the mathematical concept of

  • vectors (one-dimensional array) and
  • matrices (two-dimensional array)

multidimensional array are supported such as [x..y,z..t] and so on.

To call a variable you have to put the name of the array and the position a[1..3] and you can use it as a simple variable, but if you want to use parameters you MUST use a structure because else it will cause errors or bugs... (I do not understand, what is meant here).

If it is not possible to know the number of elements at the time of the program compilation, the dynamic array type can be used.


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring