For Programmers: Free Programming Magazines  


Home > Archive > Fortran > March 2008 > Passing a pointer to a subroutine and treating as an array?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Passing a pointer to a subroutine and treating as an array?
idharssi@hotmail.com

2008-03-12, 8:13 am

Sorry if this is a frequently asked question but I couldn't find the
answer elsewhere. Is the following piece of code, legal Fortran 90? In
particular, I pass a pointer to routine test2, but then treat it as an
array. I've tested the code with gfortran and ifort and it compiles
and gives the expected results with both.

Thanks

------------------------------------------------------------

program main
implicit none


real a(10)
real, dimension(:), pointer :: p
target :: a
integer i

common /testx/ a, p



do i=1,10
a(i)=float(i)
enddo

p => a(5:10)

write (6,*) a(5), p(1)

call test()

end

subroutine test()
implicit none

real a(10)
real, dimension(:), pointer :: p

common /testx/ a,p

write (6,*) a(5), p(1)

call test2(p)

return
end subroutine test

subroutine test2(b)
implicit none

real b(5)

write (6,*) b(1), b(2)

return
end subroutine test2
Michael Metcalf

2008-03-12, 8:13 am


<idharssi@hotmail.com> wrote in message
news:9e22c9c0-d833-44cb-8b3c-00ed2500dd58@n58g2000hsf.googlegroups.com...
> Sorry if this is a frequently asked question but I couldn't find the
> answer elsewhere. Is the following piece of code, legal Fortran 90? In
> particular, I pass a pointer to routine test2, but then treat it as an
> array. I've tested the code with gfortran and ifort and it compiles
> and gives the expected results with both.
>

I't looks fine to me too. Note, however, that you are not "passing a pointer
and using it as an array", rather you are passing an array with a pointer
attribute and using it without that attribute in the called procedure. That
is legal (see also "Fortran 95/2003 Explained", Section 5.7.1).

Regards,

Mike Metcalf


Richard Maine

2008-03-12, 7:32 pm

Michael Metcalf <michaelmetcalf@compuserve.com> wrote:

> <idharssi@hotmail.com> wrote in message
> news:9e22c9c0-d833-44cb-8b3c-00ed2500dd58@n58g2000hsf.googlegroups.com...
> I't looks fine to me too. Note, however, that you are not "passing a pointer
> and using it as an array", rather you are passing an array with a pointer
> attribute and using it without that attribute in the called procedure. That
> is legal (see also "Fortran 95/2003 Explained", Section 5.7.1).


I might further clarify that you aren't even really passing an array
with the pointer attribute. You are just passing the array that is the
target of that pointer.

I somewhat wish that pointers in Fortran hadn't been called pointers. I
think a term like "alias" would better describe them. A pointer can be
thought of as an alias (alternate name) for its target.

In *MOST* contexts - not just argument passing - when you write the name
of the pointer in code, it refers to the target. This is sometimes
described as automatically dereferencing the pointer, but I think a
simpler concept is just that the pointer is another name for the target,
in which case it seems intiutive that when you use that name, it refers
to the target.

Only in some special contexts does the pointerness come into play.
Argument passing is sometimes one of those contexts. It is perhaps more
confusing than some in that you can't tell by looking at just the actual
argument list. If the dummy argument is a pointer (which requires an
explicit interface), then you have one of the contexts in which the
pointerness of the actual argument is relevant.

In your case, the dummy argument is not a pointer, so using the name of
a pointer in the actual argument list means its target, just as using
the pointer name does in most contexts.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com