- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there a way to cast a pointer to another type.
Here is an example that does not work:
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]
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I assume you mean BOB%DUM(80). Yes, it should.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
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.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page