Menu Close

Void Pointer in FORTRAN

In Fortran, there is no direct equivalent of a “void” pointer as found in some other programming languages like C or C++. Fortran is a statically-typed language that requires explicit type declarations for variables.

However, Fortran does provide mechanisms for achieving similar functionality through the use of generic procedures and interface blocks.

One approach is to use a generic procedure that can accept arguments of different types.

Example:

In this example, the MyProcedure interface defines a generic procedure that can accept arguments of any type (class(*)). The MySubroutine and MyFunction are specific implementations of this generic procedure.

 

module MyModule
  implicit none
  
  interface MyProcedure
    module procedure MySubroutine
    module procedure MyFunction
  end interface MyProcedure
  
contains
  
  subroutine MySubroutine(arg)
    implicit none
    ! Declare the argument as a generic type
    class(*), intent(in) :: arg
    
    ! Your code here
    ! ...
    
  end subroutine MySubroutine
  
  function MyFunction(arg) result(resultVal)
    implicit none
    ! Declare the argument as a generic type
    class(*), intent(in) :: arg
    ! Declare the result variable
    integer :: resultVal
    
    ! Your code here
    ! ...
    
  end function MyFunction
  
end module MyModule

More Related Stuff