Passing a pointer to a non-pointer dummy argument

I gather that we’re allowed to pass a pointer actual argument to a non-pointer dummy argument, but doing so allows us to create aliasing situations that presumably the compiler doesn’t know about.

Consider the following example program. Even though j points to i, and the dummy arguments of sub do not have the pointer or target attributes, this compiles. Is this program standard-conforming? If so, what’s actually happening? It looks to me like I’ve just created a situation where the arguments alias, which I thought wasn’t allowed.

program example
    integer, target  :: i(10)
    integer, pointer :: j(:) => null()

        subroutine sub(p, q)
            integer, dimension(:) :: p, q
        end subroutine
    end interface

    i = 2
    j => i

    call sub(j, i)

end program

Edit: this question is related to, but different from a previous question I asked about pointers and aliasing: Fortran pointers, and aliasing

I believe it is the responsibility of the programmer to make sure this doesn’t happen. “It is not allowed” means it is undefined behavior if you do this, it does not mean the compiler will detect it and prevent you from doing it.


Thanks. Welcome to the forum!

See NOTE 2, in section Restrictions on entities associated with dummy arguments, of the Standard.

1 Like

This program can be standard conforming, depending on whether sub modifies any of the elements of the arrays p, q. If it does, it isn’t.

1 Like