Home > Archive > Fortran > February 2005 > Calling a C routine from fortran
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Calling a C routine from fortran
|
|
| binesh01@yahoo.com 2005-02-04, 9:03 am |
| I am facing the following issues in calling a C routine from fortran.
1) In the following program, array "dim" is assigned from in the file
read by the c routine.
The print statement in "print*,dim(1),dim(2),dim(3)" does not print
the correct values( It prints all zeroes).
But if i interchange the arguments of the C function, the program
works fine. Why is this?
--------------------------------------------------------
program main
implicit none
integer dim(3)
character*16 :: filen="test.P3DM"
call read_file_c(filen,dim)
print*,dim(1),dim(2),dim(3)
end program
--------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int read_file_c (char* filen,int *dim);
int read_file_c (char* filen,int *dim)
{
FILE *file;
if( ( file = fopen( filen, "r" ) ) == NULL ) {
printf("Failed to open file" );
return(1);
}
fscanf( file, "%d%d%d\n", &(dim[0]), &(dim[1]), &(dim[2]) );
fclose( file );
return(1);
}
2) Is it possible to pass a pointer to an array from fortran to C and
do the memory allocation in C and use it back in the caller fortran
routine?
Thanks in Advance,
Binesh
| |
| James Van Buskirk 2005-02-04, 9:03 am |
| <binesh01@yahoo.com> wrote in message
news:1107504902.506448.278450@l41g2000cwc.googlegroups.com...
> But if i interchange the arguments of the C function, the program
> works fine. Why is this?
> character*16 :: filen="test.P3DM"
I'm kinda surprised this works. Why not "test.P3DM"//achar(0) ?
> int read_file_c (char* filen,int *dim);
Have you tried read_file_c(char *filen, int len, int *dim); ?
--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
| |
| Sebastian Gallinat 2005-02-04, 3:59 pm |
| Maybe, a little bit outside, you should think about different concepts
of arrays in fortran and arrays and pointers in c.
Because a C-pointer is nothing then the starting adress of a memory
block, you should write in the fortran part of your program:
call read_file_c(filen,dim(1))
It would also be helpful to read careful the manual of your compiler,
topic mixed language programming.
Hope, this helps a bit,
Sebastian.
<binesh01@yahoo.com> schrieb im Newsbeitrag
news:1107504902.506448.278450@l41g2000cwc.googlegroups.com...
>I am facing the following issues in calling a C routine from fortran.
> 1) In the following program, array "dim" is assigned from in the file
> read by the c routine.
> The print statement in "print*,dim(1),dim(2),dim(3)" does not print
> the correct values( It prints all zeroes).
> But if i interchange the arguments of the C function, the program
> works fine. Why is this?
> --------------------------------------------------------
> program main
>
> implicit none
>
> integer dim(3)
>
> character*16 :: filen="test.P3DM"
>
> call read_file_c(filen,dim)
>
> print*,dim(1),dim(2),dim(3)
>
> end program
> --------------------------------------------------------
>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> int read_file_c (char* filen,int *dim);
> int read_file_c (char* filen,int *dim)
> {
> FILE *file;
> if( ( file = fopen( filen, "r" ) ) == NULL ) {
> printf("Failed to open file" );
> return(1);
> }
> fscanf( file, "%d%d%d\n", &(dim[0]), &(dim[1]), &(dim[2]) );
> fclose( file );
> return(1);
> }
>
> 2) Is it possible to pass a pointer to an array from fortran to C and
> do the memory allocation in C and use it back in the caller fortran
> routine?
>
> Thanks in Advance,
> Binesh
>
| |
| binesh01@yahoo.com 2005-02-05, 8:57 am |
| Thanks for your responses James and Sebastain.
|
|
|
|
|