Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interfacing C-Funktion wiht char* as return type

Status
Not open for further replies.

Temeshwarrior

Programmer
Sep 20, 2009
2
DE
Hello com,

im sped a lot of time with interfaing a C-Function which looks quite like this:

Code:
include "myStruct.h"

char* GetCStr(struct myStruct *myS) {
  myS->str = (char*)calloc(10,sizeof(char));
  //do some other stuff like strcpy
  return myS->str;
}

Well, i tried to write an interface, but i does not realy work (SEQFAULTS)

Code:
Interface
  Function GetCStr(myS) bind( c )
#include "myStruct.inc"
    Character, Pointer :: GetCStr
    Record /myStruct/ myS
  End Function GetCStr
End Interface

And here is the Fortran structure in myStruct.inc:

Code:
Structure /myStruct/
  Character, Pointer :: str
End Structure

And here the C-structure in myStruct.h:

Code:
struct myStruct {
  char* str;
};

Accessing the str-attribute in Fortran directly works but not via the C-function.

Is there any way to handle this?


Best,
Temeshwarrior
 
OK, it seems that there is a problem with passing the Structure to the C-Function.

If i insert a dummy argument into the C-Function like this:

Code:
char* GetCStr(int dummy, struct myStruct *myS) {
  myS->str = (char*)calloc(10,sizeof(char));
  //do some other stuff like strcpy
  return myS->str;
}

it works, dont know why because i didnt add any dummy-argument to the interface.

P.S: Im using SunStudio 12 cc and f95 compiler

Best,
Temeshwarrior

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top