0

If I have the following code (thanks to M. Chinoune) in a subroutine

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

type(ragged_array) :: raggar

allocate( raggar%vectors(2) )
allocate( raggar%vectors(1)%elements(3) )
allocate( raggar%vectors(2)%elements(4) )

raggar%vectors(1)%elements=0 
raggar%vectors(2)%elements=0 

if I want to pass raggar in an other subroutine to modify size of raggar. should I do something like :

CALL MySubroutine(raggar)

or

CALL MySubroutine(raggar%vectors%elements)

And then, in my subroutine how can I declare it ?

SUBROUTINE MySubroutine(raggar)
type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

type(ragged_array), INTENT(INOUT):: raggar

I did many tried but I always get error such as :

The type of the actual argument differs from the types of the dummy argument.

or

the shape matching rules of actual arguments and dummy argument have been violated

3
  • Place your type definition into a module and use this module in your program and subroutine. Commented Jun 6, 2017 at 16:53
  • also you have a mess with ragarr, raggarr and raggar Commented Jun 6, 2017 at 17:26
  • Oh sorry, it was error because of typing (I have one computer with internet and the other with code....) I have corrected... Commented Jun 6, 2017 at 17:32

1 Answer 1

1

Place type definitions into a module and use it in a program and subroutine.

module my
type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array
end module

program probe
use my
type(ragged_array) :: ragarr
allocate( ragarr%vectors(2) )
allocate( ragarr%vectors(1)%elements(3) )
allocate( ragarr%vectors(2)%elements(4) )
ragarr%vectors(1)%elements=0 
ragarr%vectors(2)%elements=0 
CALL MySubroutine(ragarr)
end program

SUBROUTINE MySubroutine(rr)
use my
type(ragged_array), INTENT(INOUT):: rr
end subroutine
Sign up to request clarification or add additional context in comments.

4 Comments

I already have a module with my subroutines, is it better to create two separated module ? o can I but the subroutine in the same module ? thx! (because I want to call the subroutine in an other subroutine)
This module contains the types shared by the main program and the subroutine. If you already have a module used by program and subroutine, you can place the definitions there.
Ok it work ! thanks a lot, and if I want to duplicate rr in MySubroutine is there an equivalent of size(rr) ?
I have found a way using a loop reading each size of each elements ! thanks you so much to make me understand modules ! !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.