- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How do get the size of an array passed into a subroutine? (The code below won't compile)
program sizeofff
real*8, allocatable :: fred(:)
integer :: siz
read(5,*) siz
allocate(fred(siz))
call fred1(fred)
end
subroutine fred1(fred)
real*8 :: fred(*)
write(6,*) size(fred)
return
end
program sizeofff
real*8, allocatable :: fred(:)
integer :: siz
read(5,*) siz
allocate(fred(siz))
call fred1(fred)
end
subroutine fred1(fred)
real*8 :: fred(*)
write(6,*) size(fred)
return
end
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Use something like:
module freddy
contains
module freddy
contains
subroutine fred1(fred)
real*8 :: fred(:)
write(6,*) size(fred)
return
end subroutine
end module freddy
program sizeofff
real*8, allocatable :: fred(:)
integer :: siz
read(5,*) siz
allocate(fred(siz))
call fred1(fred)
end
You need to specify an assumed-shape array - which is achieved with the colon (:).
But then the compiler needs to see the interface. Using a module is the easiest way
to do that.
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Note that SIZE returns the number of elements.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page