On 07/30/2011 05:23 AM, Ben Bacarisse wrote:
> It's almost always the calling function that determines how you must
> interface with the called language. This applies to best practice as
> much as it does to those things that are required.
>
Ok. Fortran is expecting a c-style pointer to a char. This little
snippet gives you a feel for it. This is pretty-new syntax, and what
makes it relevant to c is that fortran said *all* interop is with ISO C.
type(c_ptr):: pa
end function testc
end interface
type(c_ptr), target :: pa
character(1, c_char),pointer::fpa(

pa = testc()
....
Yes the horror of not seeing semicolons, but again I put the fortran
stuff at the top, so it can disappear with a reasonable edit.
On the C side, I think I've got something that works well, but I don't
know that without some code review. This is what dinkumware has for
strncpy; does anyone have a mnemonic for what the 'n' might mean?
strncpy
char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
The function copies the string s2, not including its terminating null
character, to successive elements of the array of char whose first
element has the address s1. It copies no more than n characters from s2.
The function then stores zero or more null characters in the next
elements to be altered in s1 until it stores a total of n characters. It
returns s1.
So I want a char * before i get to this statement, and that leaves the
malloc:
char *p = malloc(sz);
The reason that I might put it on the left is that I haven't been handed
anything that's broken, and if it doesn't draw warnings, then C might be
just fine this.
Another version might be:
char *p = (char *)malloc(sz);
, and I'm naive enough not to see anything wrong there, technically.
But as I'm sitting here, it looks like an unnecessary cast.
$ gcc -c -Wall -Wextra cfunc6.c -o cfunc.o
$ gfortran -c -Wall -Wextra caller2.f03 -o caller.o
$ gfortran -Wall -Wextra caller.o testc.o -o out
$ ./out
this sentence is less than fifty bytes.
$ cat caller2.f03
program test
use iso_c_binding
implicit none
interface
function testc() bind(c) result(pa)
use iso_c_binding
type(c_ptr):: pa
end function testc
end interface
type(c_ptr), target :: pa
character(1, c_char),pointer::fpa(

pa = testc()
call c_f_pointer(pa, fpa, [50])
print*, fpa(1:50)
end program test
! gfortran -c -Wall -Wextra caller2.f03 -o caller.o
! gfortran -Wall -Wextra caller.o testc.o -o out
$ indent -i3 cfunc6.c
$ cat cfunc6.c
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
char *
malloc_or_bust (size_t sz)
{
char *p = malloc (sz);
if (p == NULL)
{
fprintf (stderr, "malloc failed.\n");
exit (EXIT_FAILURE);
}
return p;
}
char *
testc (void)
{
size_t size = 50;
char *p = malloc_or_bust (size);
strncpy (p, "this sentence is less than fifty bytes", size - 1);
p[size - 1] = '\0
return p;
}
// gcc -c -Wall -Wextra cfunc6.c -o cfunc.o
$
Thanks for your comment. How long have I known you now? I bet that
neither of us would move back to Michigan now. what's going on in
upper-midwest states is shocking. The KKK and arsonists have showed up
in 'Sconi ten days before people were almost going to have a free and
fair election.
--
Uno