PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Free OpenACC Webinar

characters passing ocurrs error when fortran calling c

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
Landau



Joined: 27 Mar 2005
Posts: 3

PostPosted: Wed Aug 24, 2005 1:38 pm    Post subject: characters passing ocurrs error when fortran calling c Reply with quote

my main fortran code, main.f90:
Code:
program main
implicit none

write(*,*)  "abcdefghijklmnopqrstuvwxyz"
call cprint(" Hello world!")
call cprint(" Hello hell!")

end program main


"cprint" is a c subroutine here, foo.c
Code:

#include <stdio.h>
void cprint_(char* string){
printf("%s", string);
}

The result output is
Code:
 abcdefghijklmnopqrstuvwxyz
 Hello world!abcdefghijklmnopqrstuvwxyz Hello hell! Hello world!abcdefghijklmnopqrstuvwxyz

I use the debug tool "pgdbg" to check the variable passing, I found that
the fortran trans "Hello world!abcdefghijklmnopqrstuvwxyz" to string in cprint.

I can't understand this. How to solve it.
Thank you.

btw: my compile command is
-------------------------------------------------
gcc -g -c foo.c
pgf90 -g -c main.f90
pgf90 -g -o bar.out main.o foo.o
-------------------------------------------------
My pgf90 version is 5.2-1, gcc version is 3.4.3
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6218
Location: The Portland Group Inc.

PostPosted: Wed Aug 24, 2005 3:13 pm    Post subject: Reply with quote

Hi Landau,

The C printf function is expecting a null terminated character array. Fortran does not add the null character to the end of a string. When printf tries to print "Hello World!", it keeps printing values until it happens upon a 0.

To fix, add the null termination character (char(0) or '\0') at the end of your Fortran strings.

Code:
program main
implicit none

write(*,*)  "abcdefghijklmnopqrstuvwxyz"
call cprint(" Hello world! " // char(0)) ! or  " Hello world! \0"
call cprint(" Hello hell! " // char(0))  ! or " Hello hell! \0"

end program main


Code:
% gcc cprint.c -c
% pgf90 main.f90 -c
% pgf90 main.o cprint.o
% a.out
 abcdefghijklmnopqrstuvwxyz
 Hello world!  Hello hell!


Sincerely,
Mat


Last edited by mkcolg on Thu Aug 25, 2005 8:47 am; edited 1 time in total
Back to top
View user's profile
Landau



Joined: 27 Mar 2005
Posts: 3

PostPosted: Wed Aug 24, 2005 11:34 pm    Post subject: Reply with quote

Hi Mat,
That real do!
Thank you very much!

cheers,
Landau
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group