只分配一个维度有关Fortran二维数组 [英] Allocate only one dimension for a 2D array in fortran

查看:627
本文介绍了只分配一个维度有关Fortran二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个二维数组A(:,2)其中只有第一个维度的大小是未知的。
是否有可能只分配给A的第一个维度?
如果不是这样,我必须去与治疗的分配(A(N,2))每次A(:,:)。

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(:,:).

推荐答案

如果第二个维度是永远大小为2,您可以创建一个数据类型有两个变量,然后对其进行分配的数组:

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

这篇关于只分配一个维度有关Fortran二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆