6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

fortran: 構造体(type)

Posted at

下記のように,構造体の中にallocatable変数を用意して,その構造体の配列を作れば,異なる長さの配列の配列が作れる.結構便利と思う.

test.F90
program test
  implicit none
  type mytype
    integer:: larr
    real(8),allocatable:: arr(:)
  end type mytype

  integer:: i
  type(mytype):: mt(2)

  mt(1)%larr= 2
  mt(2)%larr= 3

  allocate(mt(1)%arr(at(1)%larr),&
       mt(2)%arr(at(2)%larr))

  print*, (mt(1)%arr(i),i=1,mt(1)%larr)
  print*, (mt(2)%arr(i),i=1,mt(2)%larr)
  
end program test

上のプログラムを実行すると,

$ g95 test.F90
$ ./a.out
 0. 0.
 0. 0. 0.

となり,一応ちゃんと長さの異なる配列を持つ配列ができている.

参考webサイト

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?