- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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?
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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
링크가 복사됨
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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?
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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).
