# Pointer remapping and ASSOCIATED

**URL:** <https://fortran-lang.discourse.group/t/pointer-remapping-and-associated/2956>\
**Category:** Uncategorized\
**Created:** [March 11, 2022, 1:22pm UTC](https://fortran-lang.discourse.group/t/pointer-remapping-and-associated/2956 "2022-03-11T13:22:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 11, 2022, 1:22pm UTC](https://fortran-lang.discourse.group/t/pointer-remapping-and-associated/2956/1 "2022-03-11T13:22:39Z")

</div>

For the code below, gfortran 12.0.1 20220213 and Intel Fortran 2021.5.0 Build 20211109\_000000 give different results for the last 2 cases, with ifort giving `T` and gfortran `F`. I wonder who is correct.

```auto
program test_associated ! illustrate rules at
! https://gcc.gnu.org/onlinedocs/gcc-8.4.0/gfortran/ASSOCIATED.html
implicit none
integer, parameter :: n = 4
integer, target :: i,j,k(0),m(n),a(2,2)
integer, pointer :: p => null(), q=> null(), r(:) => null(), &
  s(:) => null(), t(:) => null(), u(:) => null(), v(:) => null(), &
  w(:) => null(), x(:,:) => null(), y(:,:) => null()
p => i
r => k
s => m
t => m
u(0:3) => m
v => m(n:1:-1)
w => m(2:)
x(1:4,1:1) => a
y(1:1,1:4) => a
! comments show Intel Fortran results
print*,associated(p) ! T : p is associated
print*,associated(p,i) ! T : p is associated to scalar i
print*,associated(p,j) ! F : p is not associated to j
print*,associated(q) ! F : q is not associated
print*,associated(r) ! T : r is associated
print*,associated(r,k) ! F : the target of r is k with zero size
print*,associated(s) ! T : s is associated
print*,associated(s,m) ! T : s is associated to m with nonzero size
print*,associated(s,t) ! T : s and t both associated to m
print*,associated(s,u) ! T : s and u both associated to m
print*,associated(v,m) ! F : v associated to reverse order of m
print*,associated(w,m) ! F : w and m have different size
print*,associated(x,a) ! T : x associated to a
print*,associated(x,y) ! T : x and y associated to a
end program test_associated

```

The rules for ASSOCIATED, [according to gfortran docs](https://gcc.gnu.org/onlinedocs/gcc-4.3.6/gfortran/ASSOCIATED.html), are

> _Description_ :
> 
> `ASSOCIATED(PTR [, TGT])` determines the status of the pointer PTR or if PTR is associated with the target TGT.
> 
> _Standard_ :
> 
> F95 and later
> 
> _Class_ :
> 
> Inquiry function
> 
> _Syntax_ :
> 
> `RESULT = ASSOCIATED(PTR [, TGT])`
> 
> _Arguments_ :
> 
> | PTR | PTR shall have the `POINTER` attribute and it can be of any type. |
> | --- | --- |
> | TGT | (Optional) TGT shall be a `POINTER` or a `TARGET` . It must have the same type, kind type parameter, and array rank as PTR. |
> 
> The status of neither PTR nor TGT can be undefined.
> 
> _Return value_ :
> 
> `ASSOCIATED(PTR)` returns a scalar value of type `LOGICAL(4)` . There are several cases:
> 
> (A) If the optional TGT is not present, then `ASSOCIATED(PTR)`
> 
> is true if PTR is associated with a target; otherwise, it returns false.
> 
> (B) If TGT is present and a scalar target, the result is true if
> 
> TGT is not a 0 sized storage sequence and the target associated with PTR occupies the same storage units. If PTR is disassociated, then the result is false.
> 
> (C) If TGT is present and an array target, the result is true if
> 
> TGT and PTR have the same shape, are not 0 sized arrays, are arrays whose elements are not 0 sized storage sequences, and TGT and PTR occupy the same storage units in array element order. As in case(B), the result is false, if PTR is disassociated.
> 
> (D) If TGT is present and an scalar pointer, the result is true if
> 
> target associated with PTR and the target associated with TGT are not 0 sized storage sequences and occupy the same storage units. The result is false, if either TGT or PTR is disassociated.
> 
> (E) If TGT is present and an array pointer, the result is true if
> 
> target associated with PTR and the target associated with TGT have the same shape, are not 0 sized arrays, are arrays whose elements are not 0 sized storage sequences, and TGT and PTR occupy the same storage units in array element order. The result is false, if either TGT or PTR is disassociated.

For the line `print*,associated(x,a)` , looking at rule (C) above, since `x` and `a` have different shapes I think gfortran is correct to print `F`, but ifort says `T`. For the last line `print*,associated(x,y)`, looking at rule (E) above, I think ifort is correct to say `T` since the conditions are satisfied.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [March 11, 2022, 6:27pm UTC](https://fortran-lang.discourse.group/t/pointer-remapping-and-associated/2956/2 "2022-03-11T18:27:35Z")

</div>

The Standard (J3/18-007r1) I have in front of me has different wording.

Instead of (C) and (E) above, I see (with TARGET/TGT POINTER/PTR rewording):

```auto
Case (v): If TARGET is present and is an array target, the result is true if 
and only if POINTER is associated with a target that has the same shape as 
TARGET, is neither of size zero nor an array whose elements are zero-sized 
storage sequences, and occupies the same storage units as TARGET in array 
element order.
...
Case (vii): If TARGET is present and is an array pointer, the result is true 
if and only if POINTER and TARGET are both associated, have the same shape, 
are neither of size zero nor arrays whose elements are zero-sized storage 
sequences, and occupy the same storage units in array element order.

```

On the basis of the words of the Standard,

> [@](#):
>
> associated(x,a)

is false (different shapes) and so is

> [@](#):
>
> associated(x,y)

for the same reason. You can ask for the shapes to be printed.
