Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner

How to transport character variable from Fortran to C without transiton

Hello developers,

 

I have some question related transporting chracter variable from Fortran to C.
Here are my codes:

# In Fortran
CHARACTER URL*128
URL='http://***.**.***.**/url/something.php?inf=some&tm=200001011200'
WRITE(URL(49:59),'(A10)') YYYYMMDDHH
CALL URLOPEN(IU, URL)

# In C
urlopen(int *ret, char* url) {   
printf("%s \n", url);}

 # Result
'http://***.**.***.**/url/somethine.php?inf=some&tm=200001011200'                                    #7777END

One odd thing is that it is okay in character length under 128(127,126,..).
Is there any problem to think about in the current situation? Is there a problem if the char length in C exceeds 128?
Please let me know if there is the way to use a longer length character because the length of URL may increase.

0 Kudos
3 Replies
Highlighted
New Contributor II

C strings are null terminated

C strings are null terminated, so add a char(0) at the end of your fortran string.

There is no limit for C string length.

0 Kudos
Highlighted
Beginner

Hi Vautier,

Hi Vautier,

Thank you for your help.
I understand what you are answering but I don't know how to fix this because I am not be good at C programming enough.
Additionally I have one more question to fix my problem.

The rest of C code is below:
urlopen(int *ret, char* url)
{  
    char *prul;
    printf("%s \n", url);
    purl = _fcdtocp(url);
    handle = url_fopen(purl, "r");
}

In the case that url is "http://some_url                                    #7777END", url_fopen process does not successed.
Please let me know how to modify this purl or url string?
ex1) purl = _fcdtocp(url(0:nlength-2))
ex2) CHARACTER  URL(0:128)
       URL="http://***.**.***.**/url/something.php?inf=some&tm=200001011200"
       URL(0)=" "
ex3) or something


Thank you,
Kihang

0 Kudos
Highlighted
New Contributor II

In the fortran code, add /

In the fortran code, add //char(0) at the end of the url= statement.

Nothing to do on C side.

0 Kudos