Software Archive
Read-only legacy content
17061 Discussions

assigning pointers to a member of a data type

Intel_C_Intel
Employee
431 Views
Hi

I'm having a problem outlined by the following code. I can assign a pointer to a variable within a data type but when it comes to checking if the pointer has been associated with the target a compiler error results. In the following code the pointer "x" can be associated with the data member "thing.dble" however the call to "( associated( x, thing.dble ) )" gives the error.

Any help would be appreciated
Allan

 
program tester  
implicit none  

type thingy  
real*8 :: dble  
integer :: int  
end type  
  
real*8, pointer :: x => null()  
real*8, target :: y   
type(thingy), target :: thing  
  
thing.dble = 10  
! pointer "x" can be associated with target "thing.dble"  
  
x => thing.dble  
  
! compiler will not allow the following line to check if the pointer "x"   
! is associated with the target  
  
if ( associated( x, thing.dble ) )then  
 print*, ' "x" is associated with thing.dble'  
 end if  
  
end program
0 Kudos
3 Replies
Steven_L_Intel1
Employee
431 Views
This is an asymmetry in the language - while you can do pointer assignment to a subcomponent of a pointer/target, you can't use ASSOCIATED on the same thing.

Steve
0 Kudos
Intel_C_Intel
Employee
431 Views
It seems to me that this asymmetry in the language must be due to some interpretation by the committee regarding the ASSOCIATED inquiry intrinsic because I can't see where it crops up in section 13.14.13 of the F95 standard. In the appended program, I would have guessed from 13.14.13 that Test #1:2 should both be valid F95 because the structure component and array element are both scalars; as noted above Test #1 flunks but Test #2 passes. Further I would have expected Test #3 to fail because z(1), while it dereferences the pointer z, is not itself a pointer (the statement z(1) => array(2) is illegal, for example) and Test #4 would also fail (try z(1:3) => array(2:4)) and while test #5 fails, I would have thought its results should be consistent with Test #3:4. Where can we find the interpretation that allows us to predict these results?

 
!!DEC$ DEFINE CAUSES_ERROR 
program tester    
   implicit none 
   integer, parameter :: dp = selected_real_kind(15,300) 
   type thingy    
      real(dp) :: dble 
      integer :: int    
   end type thingy 
   real(dp), pointer :: x => null()    
   real(dp), target :: y     
   type(thingy), target :: thing 
   real(dp), target :: array(10) 
   real(dp), pointer :: z(:) => null() 
   integer i 
   type(thingy), pointer :: w 
 
   thing%dble = 10    
! pointer "x" can be associated with target "thing.dble" 
   x => thing%dble 
! compiler will not allow the following line to check if the pointer "x"     
! is associated with the target 
! Test #1 
!DEC$ IF DEFINED(CAUSES_ERROR) 
   if ( associated( x, thing%dble ) )then    
      print*, ' "x" is associated with thing%dble' 
   end if 
!DEC$ ELSE 
   print*, ' "x" cannot be tested for association with thing%dble' 
!DEC$ENDIF 
! Try the same thing with an array element 
   array(2) = 10 
   x => array(2) 
! Test #2 
   if ( associated( x, array(2) ) )then    
      print*, ' "x" is associated with array(2)'    
   end if 
! Try with an array section 
   array = (/(i, i = 1, size(array))/) 
   z => array(2:6) 
! Test #3 
   if ( associated( z(1), array(2) ) )then    
      print*, ' ""(1)" is associated with array(2)'    
   end if 
! Test #4 
   if ( associated( z(1:3), array(2:4) ) )then    
      print*, ' "z(1:3)" is associated with array(2:4)' 
   end if 
! Try with pointer to structure 
   thing%int = 42 
   x => thing%dble 
   w => thing 
! Test #5 
!DEC$ IF DEFINED(CAUSES_ERROR) 
   if ( associated( w%dble, x ) )then    
      print*, ' "w%dble" is associated with x' 
   end if 
!DEC$ ELSE 
   print*, ' "w%dble" cannot be tested for association with x' 
!DEC$ENDIF 
end program tester 
0 Kudos
Steven_L_Intel1
Employee
431 Views
Hmm - looking at this again, I may have misunderstood the text and implied an asymmetry that wasn' there. I'll check into this.

Steve
0 Kudos
Reply