Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare array of allocatable scalars in Fortran?

Allocatable arrays are possible in Fortran 90 and up.

INTEGER, ALLOCATABLE, DIMENSION(:) :: test_int_array

Allocatable scalars such as allocatable characters are possible in Fortran 2003.

CHARACTER(LEN=:), ALLOCATABLE :: test_str

I am wondering is it possible to declare an array, fixed or allocatable, of allocatable characters? (Possibly like something below, which does not compile unfortunately.)

CHARACTER(LEN=:), ALLOCATABLE, DIMENSION(4) :: test_str_array
like image 541
SOUser Avatar asked Dec 06 '11 16:12

SOUser


1 Answers

    program test_alloc

   character (len=:), allocatable :: string

   character(len=:), allocatable :: string_array(:)

   type my_type
      character (len=:), allocatable :: my_string
   end type my_type
   type (my_type), dimension (:), allocatable :: my_type_array

   string = "123"
   write (*, *) string, len (string)
   string = "abcd"
   write (*, *) string, len (string)

   allocate(character(5) :: string_array(2))
   string_array (1) = "1234"
   string_array (2) = "abcde"
   write (*, *) string_array (1), len (string_array (1))
   write (*, *) string_array (2), len (string_array (2))

   allocate (my_type_array (2))
   my_type_array (1) % my_string = "XYZ"
   my_type_array (2) % my_string = "QWER"
   write (*, *) my_type_array (1) % my_string, len (my_type_array (1) % my_string)
   write (*, *) my_type_array (2) % my_string, len (my_type_array (2) % my_string)

end program test_alloc

I found the syntax at http://software.intel.com/en-us/forums/showthread.php?t=77823. It works with ifort 12.1 but not with gfortran 4.6.1. Trying the work around of creating a user-defined type didn't work either.

like image 177
M. S. B. Avatar answered Nov 03 '22 01:11

M. S. B.