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!

pointer to available datatype

Status
Not open for further replies.

ya0037

Programmer
Oct 7, 2010
30
DE
Hellow everybody,

I am wondering whether there would be a way in FORTRAN to get a pointer address to an available datatype.

For example:
I have the below data type
Code:
module dt
type data
integer, dimension(3) :: comp
real, dimension(:), pointer :: a => null
end type 
end module dt
and I would create
Code:
program test
use dt
implicit none
type(data) :: mytype
end program test

Now I am wondering defining mytype, is there anyway I can get a pointer address to this type.

Thanks a lot in advance.
Cheers,



 
What have you tried? Have you read up on Fortran 90 Pointers at all?

I am not even sure what your are asking; in fact, your question seems mis-constructed to me..."pointer address"? Did you mean to say "address pointer"?

To tell you the truth, I am yet to use pointers in Fortran; but, a bit of reading triggered by your post revealed that pointers and addresses are 2 different things and, apparently, Fortran (unlike C) tends to hide such inner-details from the programmer...this has been on purpose, by the way, and that is why it has been very difficult to produce memory-related problems in Fortran.

Getting back to your "pointer address":
Do you really need the address? or
Do you just need the ability to use pointers?
 
Code:
module dt
  type data
    integer, dimension(3) :: comp
    real, dimension(:), pointer :: a => null() ! notice the difference with your version
  end type 
end module dt 

program test
  use dt
  implicit none
  type(data),target :: mytype
  type(data),pointer :: mytype2
  real, dimension(:), pointer :: vect
  mytype2=> mytype ! mytype2 becomes an alias of mytype
  allocate(vect(10)) ! usually, I only allocate allocatable objects but for you ...
  vect=5
  mytype2%a => vect
  write(*,*) mytype%a ! I hope 10 values equal to 5
end program test

François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top