Skip to main content
Engineering LibreTexts

14.2: Array Declaration

  • Page ID
    54353
  • \( \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}}\)

    Multidimensional array declaration is very similar to single-dimension array declaration. Arrays must be declared before use. The type of the array is defined followed by the size or dimension, which in this case requires a size for each dimension. As before, 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 of an array declaration is,

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

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

    For example, to declare a two-dimensional array 100 by 100,

    integer, dimension(100,100) :: nums1
    

    will create an array, nums1, with space for a total of 10,000 integer values.

    In this example, the extent for each dimension is 100, which means that each of the two dimension's indexes will range form 1 to 100. Each or both extents can be changed by specifying the extents as:

    (smaller-integer:larger-integer, smaller-integer:larger-integer)
    

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

    integer, dimension(0:9,0:9) :: ranges
    

    will create an array, ranges, with both indexes between 0 and 9 (inclusive). Using index values not within the specified range will result in an error.

    Dynamic Declaration

    The same as single-dimension, a dynamic declaration means that the dimension of the array can be set when the program is executed. Once set, the dimensions cannot be altered. When using a dynamic declaration, the array type and name must be defined, which specifies only the name and type of the array, but does not reserve any space for the array. Then, during program execution, the array can be allocated which 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>, <dimension>, 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. As before, 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 :: allstat
    

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

    allocate(nums2(100,100), stat=allstat)
    

    The size, 100 by 100 in this example, can be a parameter or variable, but it must be an integer. The variable allstat will be set to 0 if the allocation is successful and >0 if the allocation failed.


    This page titled 14.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.