Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers to subroutines in FORTRAN [duplicate]

Possible Duplicate:
Function pointer arrays in Fortran
How to alias a function name in Fortran

In FORTRAN, how I can create and use a pointer, which points to a subroutine?

Furthermore, is it possible to have a hole array of pointers pointing in various subroutines?

I know that these things can be easily implemented in C, but what about FORTRAN?

EDIT

I have tried to use the command:

PROCEDURE (), POINTER :: pMYSUB => NULL()

I made pMYSUB pointer to point at the subroutine:

pMYSUB => MYSUB 

I have also put MYSUB subroutine into INTERFACE:

INTERFACE 
   SUBROUTINE MYSUB 
   END SUBROUTINE
END INTERFACE

MYSUB subroutine has no arguments. The problem is that when I use:

call pMYSUB

I get the linking error: unresolved external symbol _pMYSUB. What I am doing wrong? The command:

POINTER(pMYSUB, MYSUB)

is another way of making the point pMYSUB to point at the subroutine MYSUB?

like image 468
helios21 Avatar asked Feb 22 '23 22:02

helios21


2 Answers

Function pointers in Fortran are called "procedure pointers", part of the Fortran 2003 standard. Many modern compilers support them nowadays. There's also a very limited form of function pointer going back to at least F77, where you can have a procedure argument which is a procedure; you cannot have normal function pointer variables before F2003 though. If you have problems even after googling up something based on the above, post some code of yours that you're writing and I'm sure someone will help you out.

Wrt. an array of pointers, that is for some reason not allowed. The common work-around is to create a derived type with a pointer component, then make an array of these derived types.

like image 52
janneb Avatar answered Mar 27 '23 05:03

janneb


The problem was that my subroutine's name was DO_CALC, and for some reason the statement:

PROCEDURE (DO_CALC), POINTER :: pDO_CALC => NULL()

didn't like to the compiler. I changed my subroutine's name and now works OK!

@Janneb, nice idea to use an array of derived types instead of an array of function pointers which is isn't allowed in Fortran.

like image 36
helios21 Avatar answered Mar 27 '23 04:03

helios21