Difference between revisions of "Array"

From Free Pascal wiki
Jump to navigationJump to search
(Static and Dynamic array sections)
Line 1: Line 1:
 
{{Array}}
 
{{Array}}
  
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|real]] or any other type including user defined types.
+
An '''array''' is a type that groups a number of [[Variable|variables]] of the same type. Examples are an array of [[Char|char]], an array of [[Integer|integer]], and an array of [[Real|real]]. In fact, any type, including user defined types, may be used in an array. However, the elements of an array are always of the same type. Different types cannot be grouped into an array. For this purpose, see [[Record|record]]s.
  
Different types of variables cannot be grouped into an array. For this purpose, see [[Record|record]]s.
+
==Static Arrays==
 
+
The declaration works similar to that for simple types, but you need to add the number of elements via an index range, as well as the array element type.
 
 
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 49: Line 45:
 
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).
 
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.
+
==Dynamic Arrays==
 +
If it is not possible to know the exact number of array elements needed at the time of the program compilation, the [[Dynamic array|dynamic array]] type can be used. A dynamic array can grow or shrink in size during program execution.
  
 
{{Data types}}
 
{{Data types}}
 
[[Category:FPC]]
 
[[Category:FPC]]

Revision as of 09:01, 12 July 2016

Deutsch (de) English (en) español (es) suomi (fi) français (fr) Bahasa Indonesia (id) 日本語 (ja) русский (ru) 中文(中国大陆)‎ (zh_CN)

An array is a type that groups a number of variables of the same type. Examples are an array of char, an array of integer, and an array of real. In fact, any type, including user defined types, may be used in an array. However, the elements of an array are always of the same type. Different types cannot be grouped into an array. For this purpose, see records.

Static Arrays

The declaration works similar to that for simple types, but you need to add the number of elements via an index range, as well as the array element type.

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

startindex must be less than or equal to endindex, and both must resolve to an integer constant, either an integer value or a const value that is an integer. Either or both numbers may be negative or zero.


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

Dynamic Arrays

If it is not possible to know the exact number of array elements needed at the time of the program compilation, the dynamic array type can be used. A dynamic array can grow or shrink in size during program execution.


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