gFortran error "Unexpected pointer assignment"

Unexpected pointer assignment statement in …

If you get this and the erring file is a MODULE, then you may forgot to have the assignments below in the CONTAINS section. What you are assigning to must be a TARGET.

Further, the assignments must be in a SUBROUTINE or FUNCTION.

If you were converting a set of F77 EQUIVALENCEs to pointer assignments, you could easily overlook MODULE … CONTAINS … process … your_pointer => (not ==>) fortran_storage_with_TARGET_attribute

The following code compiles with gfortran 12 (and other versions too, I think). The pointer initialization is “above” the contains, which in this small module doesn’t exist. This is one way to “modernize” f77 equivalences in legacy code. This is roughly the same as the compile time equivalence (ip,ival).

module xxx
   integer, save, target :: ival
   integer, pointer :: ip => ival
end module xxx
1 Like