- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Like in C, argument with integer data type can be regarded as a pointer to somthing that can be defined.
And how could the code be?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If a sub is as follows, how can i make it works unless to change the type of i.
subroutine sub(i)
integer(C_PTR) :: i
!........
end subroutine
main()
type(mytype)::mt
call sub(LOC(mt))
end
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes. You have TYPE(C_PTR), which is more or less the same thing, or a CLASS(*) pointer, which is equivalent in the sense that it can point to anything.
Which is appropriate depends on what you are trying to do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I meant: If a variable's address is transfered to a sub and the argument data type is integer(C_PTR). And if I know the variable's data type, how can I use the address?
The idea is too much C style i think.
In fortran, how can I change a pointer's type?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If a sub is as follows, how can i make it works unless to change the type of i.
subroutine sub(i)
integer(C_PTR) :: i
!........
end subroutine
main()
type(mytype)::mt
call sub(LOC(mt))
end
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
PROGRAM p
IMPLICIT NONE
TYPE :: mytype
INTEGER :: component
END TYPE mytype
CALL main
CONTAINS
SUBROUTINE main
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_LOC
TYPE(mytype), TARGET :: mt
mt%component = 666
CALL sub(C_LOC(mt))
END SUBROUTINE main
SUBROUTINE sub(the_c_ptr)
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_F_POINTER
TYPE(C_PTR), INTENT(IN) :: the_c_ptr
TYPE(mytype), POINTER :: ptr_to_mt
CALL C_F_POINTER(the_c_ptr, ptr_to_mt)
PRINT *, ptr_to_mt%component
END SUBROUTINE sub
END PROGRAM p
C_LOC gives you a C address (a C pointer), C_F_POINTER lets you associate a Fortran POINTER with the thing at that C address.
If you are not interoperating with C, there are very few reasons why you would want to be using this sort of stuff.
For Fortran CLASS(*) pointers (and for polymorphic pointers in general), you can use SELECT TYPE to access the object being pointed at as its dynamic type (or some parent type of that dynamic type).
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page