Re: busting sp datatypes
- From: "James Van Buskirk" <not_valid@xxxxxxxxxxx>
- Date: Fri, 29 Aug 2008 11:55:02 -0600
"Richard Maine" <nospam@xxxxxxxxxxxxx> wrote in message
news:1imfh28.sw7q2dj88rkN%nospam@xxxxxxxxxxxxxxxx
LR <lruss@xxxxxxxxxxxxx> wrote:
Ok, thanks for that. So is there a nice way to sort character*7 calling
C's qsort from Fortran?
I haven't thought much about it. I wouldn't normally use C's qsort
anyway. Probably C_LOC is the simplest way. Note that this is not like
the other recent thread where C_LOC was suggested as a way to avoid
array descriptor passing. In that case, C_LOC was just a confusing and
unhelpful diversion. TYpe cheating is a different matter.
Possible implementation:
C:\gcc_mingw64a\clf\qsort>type qsort.f90
module funcs
implicit none
integer, private :: my_len = 7
interface
subroutine qsort(BasePtr, NoOfElements, Width, cmp_func) bind(C)
use ISO_C_BINDING
! import my_cmp_func ! doesn't work
type(C_PTR), value :: BasePtr
integer(C_SHORT), value :: NoOfElements
integer(C_SHORT), value :: Width
interface
function cmp_func(elem1, elem2) bind(C)
use ISO_C_BINDING
integer(C_LONG) cmp_func
type(C_PTR), value :: elem1
type(C_PTR), value :: elem2
end function cmp_func
end interface
! procedure(my_cmp_func) cmp_func ! doesn't work
end subroutine qsort
end interface
contains
function my_cmp_func(elem1, elem2) bind(C)
use ISO_C_BINDING
integer(C_LONG) my_cmp_func
type(C_PTR), value :: elem1
type(C_PTR), value :: elem2
character(my_len), pointer :: p1
character(my_len), pointer :: p2
call C_F_POINTER(elem1, p1)
call C_F_POINTER(elem2, p2)
if(p1 < p2) then
my_cmp_func = -1
else if(p1 == p2) then
my_cmp_func = -0
else ! p1 > p2
my_cmp_func = 1
end if
end function my_cmp_func
end module funcs
program test
use funcs
use ISO_C_BINDING
implicit none
integer, parameter :: prog_len = 7
integer(C_SHORT) NoOfElements
character(prog_len), target, allocatable :: jabberwocky(:)
integer(C_SHORT) Width
NoOfElements = 22
Width = prog_len
allocate(jabberwocky(NoOfElements))
jabberwocky = [character(prog_len) :: "beware", "the", "jabberwock", &
"my", "son", "the", "jaws", "that", "bite", "the", "claws" , &
"that", "catch", "beware", "the", "jubjub", "bird", "and", "shun", &
"the", "frumious", "bandersnatch"]
write(*,'(10(a:1x))') jabberwocky
call qsort(C_LOC(jabberwocky(1)(1:1)),NoOfElements,Width,my_cmp_func)
write(*,'(/10(a:1x))') jabberwocky
end program test
C:\gcc_mingw64a\clf\qsort>x86_64-pc-mingw32-gfortran qsort.f90 -oqsort
C:\gcc_mingw64a\clf\qsort>qsort
beware the jabberw my son the jaws that bite the
claws that catch beware the jubjub bird and shun the
frumiou banders
and banders beware beware bird bite catch claws frumiou
jabberw
jaws jubjub my shun son that that the the the
the the
--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
.
- References:
- Re: busting sp datatypes
- From: glen herrmannsfeldt
- Re: busting sp datatypes
- From: wim
- Re: busting sp datatypes
- From: Ron Ford
- Re: busting sp datatypes
- From: Ron Shepard
- Re: busting sp datatypes
- From: Richard Maine
- Re: busting sp datatypes
- From: glen herrmannsfeldt
- Re: busting sp datatypes
- From: LR
- Re: busting sp datatypes
- From: Richard Maine
- Re: busting sp datatypes
- From: LR
- Re: busting sp datatypes
- Prev by Date: Re: busting sp datatypes
- Next by Date: Re: Conversion from int to char
- Previous by thread: Re: busting sp datatypes
- Next by thread: Re: busting sp datatypes
- Index(es):
Relevant Pages
|