Skip to main content
Engineering LibreTexts

13.2: Array Declaration

  • Page ID
    54345
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    An array must be declared before use. The type of the array is defined followed by the size or dimension. There are two ways to declare an array; static and dynamic.

    Static Declaration

    A static declaration means that the size or dimension of the array must be defined initially (before the program is compiled). The size definition cannot be altered. The general form an array declaration is,

    type, dimension(extent) :: name1, name2, ..., nameN
    

    where type is the data type (integer, real, etc.) of the array. The dimension specifies the extent or size, and name1, name2, ..., nameN are names of one or more arrays being declared.

    For example, to declare an array of 1000 integers,

    integer, dimension(1000) :: nums1
    

    will create an array, nums1, with space for 1000 integer values.

    In this example, the extent is 1000 which means that the array indexes will range from 1 to 1000. The extent can be changed by specifying the extent as:

    smaller-integer : larger-integer
    

    When only one number is specified, the smaller-integer is assumed to be 1. When both numbers are specified, the array index's will range between the smaller-integer and the larger-integer. For example, a declaration of:

    integer, dimension(-5:5) :: ranges
    

    will create an array, ranges, with indexes between -5 and 5 (inclusive). Using index values not within the specified range will result in an error. If the compiler option, fcheck=bounds, to verify subscript bounds is used, this error will be trapped at run-time. However, if the run-time bounds checking is not turned on, the error may not be obvious and could result in unrelated problems such as other variables being over-written or even security holes in some cases.

    Static Array Declaration

    Static array declarations are appropriate when the maximum size of the array is known ahead of time. The size of a static array is set by the declaration and once the array is declared, the size cannot be changed. Optionally, the array value can be initialized by the declaration. For example, to declare an array named costs with 4 elements and set the four elements to 10.0, 15.0, 20.0, and 25.0,

    real, dimension(4) :: costs=(/10.0, 15.0, 20.0, 25.0/)
    

    The numbers, enclosed between the “/”s are assigned in order. So, costs(1) is set to 10.0, costs(2) is set to 15.0, costs(3) is set to 20.0, and costs(4) is set to 25.0.

    Additionally, after declaration, it is possible to initialize all elements of an array to a single value with an assignment statement. For example,

    costs = 0.0
    

    will set all four elements of the costs array to zero.

    Dynamic Array Declaration

    A dynamic declaration means that the size or dimension of the array can be set when the program is executed. Dynamic array declarations are appropriate when the maximum size of the array is not known ahead of time and can be determined based on information obtained when the program is executing. However, once set, the size cannot be altered. When using a dynamic declaration, the array type and name must be defined. This only specifies the name and type of the array, but does not reserve any space for the array. During the program execution, the array must be allocated. The allocation will create the space for the array. Only after the array has been allocated can it be used.

    For example, to declare an array,

    integer, dimension(:), allocatable :: nums2
    

    reserves the name nums2, but does not reserve any space for values.

    Dynamic Array Allocation

    To allocate the space for the array, the allocate statement must be used. Before an array can be allocated, it must be declared as allocatable. The general form of the allocate statement is:

    allocate(<array name>, stat=<status variable>)
    

    The status variable must be an integer variable and will be used by the system to place a status code indicating the status (success or failure) of the operation. If the status variable is set to 0, the allocation was successful. If the status variable is set to >0, an error occurred and the allocation was not successful.

    For example, given the declarations,

    integer, dimension(:), allocatable :: nums2
    integer :: allst
    

    the following allocate statement allocates space for 1000 numbers in array nums2,

    allocate(nums2(1000), stat=allst)
    

    The size, 1000 in this example, can be a variable, but it must be an integer. The status variable allst will be set to 0 if the allocation is successful. However, if the status variable allst is set to a value >0, an error occurred and the allocation was not successful.


    This page titled 13.2: Array Declaration is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?