booksee
booksee

Reputation: 401

Allocate only one dimension for a 2D array in fortran

Suppose I have a 2D array A(:,2) where only the size of first dimension is unknown. Is it possible to allocate only for the first dimension of A ? If not, I have to go with " allocate(A(n,2)) " each time by treating A as A(:,:).

Upvotes: 1

Views: 2226

Answers (1)

David Trevelyan
David Trevelyan

Reputation: 166

If the second dimension is always of size 2, you could create a data type with two variables and then allocate an array of them:

program main
    implicit none

    type two_things
        integer :: first
        integer :: second
    end type two_things

    type(two_things), dimension(:), allocatable :: A

    allocate(A(100))

    A(1)%first = 1
    A(1)%second = 2

    print*, A(1)%first, A(1)%second, shape(A)

    deallocate(A)

end program main

Upvotes: 4

Related Questions