Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arrays as pointers

Status
Not open for further replies.

marrisa

Technical User
Nov 26, 2004
8
TT
Dear members,

I am currently passing an allocatable array into a function however I am getting a multitude of warnings. I think the solution is to pass the array into the function as a pointer ghowever I have no experience with this matter. I would be greatful if someone could show me how this is done..

Thank you in advance
marrisa
 
Could you paste a sample of your code which is giving all these warnings.

Here is an example of using allocate/deallocate
Code:
module ARayOfHope
contains
subroutine fillarr (arr, arrmax)
   ! Size of array unknown to compiler
   integer, intent(inout)::arr(:)
   integer, intent(in)::arrmax
   integer:: i
   do i = 1, arrmax, 1
      arr(i) = i * i
   enddo

   return
end subroutine

subroutine dumparr (arr, arrmax)
   ! Size of array unknown to compiler
   integer, intent(in)::arr(:)
   integer, intent(in)::arrmax
   integer:: i
   do i = 1, arrmax, 1
      print *, arr(i)
   enddo
   return
end subroutine
end module

module BlockData
   ! Declare an allocatable array
   integer, dimension(:), allocatable :: dyn
   integer::dynmax
   parameter (dynmax = 10)
end module

program main
   use ARayOfHope
   use BlockData
   integer::i
   !
   ! allocate space
   allocate (dyn(dynmax))
   !
   ! do something with the array
   call fillarr (dyn, dynmax)
   call dumparr (dyn, dynmax)
   !
   ! deallocate the space
   deallocate (dyn)
   stop
end program
 
Example of pointer usage
Code:
program main
   integer, pointer:: iptr(:)
   integer:: saymax
   parameter (saymax = 10)
   integer, target:: say(saymax)
   integer:: i

   ! Fill an array
   do i = 1, saymax, 1
      say(i) = i * i
   enddo

   ! Pointer is from element 2:saymax - 1
   iptr => say(2:saymax - 1)
   ! Use the pointer like any array
   do i = 1, saymax - 2, 1
      print *, i, ' = ', iptr(i)
   enddo
   stop
end program
[/CODE]
 
The subroutine is very long however it is of the following form....

subroutine GenData(num)

external function1

integer num
real, allocatable :: Array1:)), Array2:))

allocate(Array1(num))

Array2 = function1(Array1, num)

end subroutine

I didnt need to use a pointer after all, the porblem was that I wasnt using an explicit interface in the subroutine...A call to a routine which returns an array, or which has a deferred-shape array argument MUST have an explicit interface visible to the caller...

Thanks xwb for your help...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top