Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
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.
29282 Discussions

Does fotran has something like (void*) pointer?

An_N_1
New Contributor I
967 Views

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 Kudos
1 Solution
An_N_1
New Contributor I
967 Views

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 

 

View solution in original post

0 Kudos
4 Replies
IanH
Honored Contributor III
967 Views

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 Kudos
An_N_1
New Contributor I
967 Views

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 Kudos
An_N_1
New Contributor I
968 Views

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 Kudos
IanH
Honored Contributor III
967 Views
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 Kudos
Reply