Pointer in a select type

Is this standard conforming, both gfortran and ifx compile it, so it should be. But who ever knows with pointers…

module tst_mod
implicit none

type pippo
  integer :: z
end type

type,extends(pippo) :: minnie
  real :: x
end type

contains
subroutine look(p)
  class(pippo),target :: p
  type(minnie), pointer :: p_ptr

  select type (a => p)
  type is (minnie)
    p_ptr => a
  class default
    error stop
  end select

  p_ptr%x = 45.0

end subroutine
end module

That will be handful as with many passed objects one don’t have to nest one select type inside another.

1 Like

My first impression is that yes, it’s standards conforming. nagfor also does not complain about it, which lends pretty strong credence.

I’m a bit confused. Isn’t that just a name association?

I’m not sure which aspect of this code you’re questioning, but it looks valid to me. In this example, p is the selector and a the associating entity. The standard (F2023 here) says (11.1.3.3 Other attributes of associate names):

The associating entity does not have the ALLOCATABLE or POINTER attributes; it has the TARGET attribute if and only if the selector is a variable and has either the TARGET or POINTER attribute.

Since p has the TARGET attribute, so does a, and that makes it valid as the target in a pointer assignment.

I was just worrying that the pointer may become undefined after the end of the select type.

I didn’t think so but I wanted a confirmation.

The pointer’s scope is outside of the SELECT TYPE.

1 Like