Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Cast Pointer to another type

merik
Beginner
1,016 Views
Is there a way to cast a pointer to another type.

Here is an example that does not work:
[cpp]type ABC   
CHARACTER*1 DUM(100)
end type ABC
type(ABC), pointer :: BOB 
      
integer*4 ,pointer :: F1

F1 => BOB%DUM(80)[/cpp]
0 Kudos
6 Replies
Steven_L_Intel1
Employee
1,016 Views
You sure you want F1 to be just an integer scalar? Anyway, here's how you do it.
[cpp]use, intrinsic :: iso_c_binding
type ABC
CHARACTER*1 DUM(100)
end type ABC
type(ABC), pointer :: BOB
integer*4, pointer :: F1
call C_F_POINTER(C_LOC(BOB), F1)[/cpp]

0 Kudos
merik
Beginner
1,016 Views
You sure you want F1 to be just an integer scalar? Anyway, here's how you do it.
[cpp]use, intrinsic :: iso_c_binding
type ABC
CHARACTER*1 DUM(100)
end type ABC
type(ABC), pointer :: BOB
integer*4, pointer :: F1
call C_F_POINTER(C_LOC(BOB), F1)[/cpp]


I am trying to force F1 to use the four bytes from DUM at the 80th position. I would perfer a scalar. I'm not able to try it right now but will this work with call C_F_POINTER(C_LOC(BOB(80)), F1)
0 Kudos
Steven_L_Intel1
Employee
1,016 Views
I assume you mean BOB%DUM(80). Yes, it should.
0 Kudos
merik
Beginner
1,016 Views

Yes, you are correct. This worked fine. I am trying to do the same thing for arrays but that does not seem to work.

I did:


integer*4, pointer :: F2(:)
call C_F_POINTER(C_LOC(BOB%DUM(84)), F2)

But I get an exception (Access violation) with C_F_POINTER. Any clues?
0 Kudos
Steven_L_Intel1
Employee
1,016 Views
I'd need to see a complete example. Did you specify the SHAPE argument to C_F_POINTER? This works:

[cpp] use, intrinsic :: iso_c_binding 
 type ABC 
 CHARACTER*1 DUM(100) 
 end type ABC 
 type(ABC), pointer :: BOB 
 integer*4, pointer :: F1(:)
 allocate (BOB)
 do i=1,100
 BOB%DUM(i:i) = char(i)
 end do
 call C_F_POINTER(C_LOC(BOB%DUM(84)), F1,[4])  
 print *, F1(1)
 end[/cpp]
Did you allocate BOB?
0 Kudos
merik
Beginner
1,016 Views
I'd need to see a complete example. Did you specify the SHAPE argument to C_F_POINTER? This works:

[cpp] use, intrinsic :: iso_c_binding 
 type ABC 
 CHARACTER*1 DUM(100) 
 end type ABC 
 type(ABC), pointer :: BOB 
 integer*4, pointer :: F1(:)
 allocate (BOB)
 do i=1,100
 BOB%DUM(i:i) = char(i)
 end do
 call C_F_POINTER(C_LOC(BOB%DUM(84)), F1,[4])  
 print *, F1(1)
 end[/cpp]
Did you allocate BOB?

Yes, I allocate BOB in C. The actual code is:


[cpp]      module mymod   
      use, intrinsic :: ISO_C_BINDING   
      implicit none
      
      type, bind(C) :: xrftest_t   
        CHARACTER*1 DUM(100)
      end type xrftest_t
      type(C_PTR), bind(C) :: xrftest
!ms$attributes dllimport :: xrftest
cDEC$ ATTRIBUTES EXTERN  :: XRFTEST
      type(xrftest_t), pointer :: BOB 
      
      integer*4 ,pointer :: F1
      integer*4, pointer :: F2(:)

      end module mymod  
      
      SUBROUTINE  FUNNY()
      use, intrinsic :: ISO_C_BINDING   
      USE mymod
!MS$ ATTRIBUTES DLLEXPORT :: FUNNY
C
      IMPLICIT NONE
      

      ! Convert C pointer to Fortran pointer   
      call C_F_POINTER(xrftest, BOB)
      call C_F_POINTER(C_LOC(BOB%DUM(81)), F1)
      call C_F_POINTER(C_LOC(BOB%DUM(85)), F2,[4]) 
      
      F1 = 77
     
      F2(1) = 88
      F2(2) = 99
      F2(3) = 101
      F2(4) = 102
   
      END
      [/cpp]
Thanks, It works great.
0 Kudos
Reply