Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
공지
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Does fotran has something like (void*) pointer?

An_N_1
새로운 기여자 I
976 조회수

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?

0 포인트
1 솔루션
An_N_1
새로운 기여자 I
976 조회수

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 

 

원본 게시물의 솔루션 보기

0 포인트
4 응답
IanH
명예로운 기여자 III
976 조회수

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.

0 포인트
An_N_1
새로운 기여자 I
976 조회수

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? 

0 포인트
An_N_1
새로운 기여자 I
977 조회수

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 

 

0 포인트
IanH
명예로운 기여자 III
976 조회수
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).

0 포인트
응답