I am trying to pass a string array to my C function using FFI as
var ffi = require('ffi');
var ref=require('ref');
var ArrayType = require('ref-array');
var Struct = require('ref-struct');
var StringArray = ArrayType('string');
var myStructure=Struct({
'ulonglong' : ref.types.ulonglong,
'charptrptr' : StringArray,
'stringArr' : StringArray
});
var structObjNew=new myStructure();
structObjNew.ulonglong=123456;
structObjNew.charptrptr=["sample","ptrptr"];
structObjNew.stringArr=["sample1","ptrptr1"];
var libfactorial = ffi.Library('./stringArray', {
'readArray': [ 'string', [ myStructurePtr]] //creating header definition
});
var output=libfactorial.readArray(structObjNew.ref());
.C file for the above code
struct myStructure
{
unsigned long long ulonglong;
char **charptrptr;
char stringArr[3][10];
};
EXPORT char* readArray(struct myStructure *nameofstr) {
printf("Value from first param from structure of type char**\n");
printf("%s\n",nameofstr->charptrptr[0]);
printf("%s\n",nameofstr->charptrptr[1]);
printf("value form second param from structure of thype char[][]\n");
printf("%s\n",nameofstr->stringArr[0]);
printf("%s\n",nameofstr->stringArr[1]);
}
I am getting output like:
The value of the first param from structure of type char**
Sample: ptrptr
The value of the second param from a structure of type char[][]
Any garbage value like ▲é┌
Ie When I am passing an array to char**
type then I am getting a correct output, but when I am trying to pass it to char*[]
or char[][]
format declared inside structure , I am getting a garbage value.
But directly passing string array like ["aa","bb"]
to char*[]
parameter in C works fine. It is not working for me when I am using it inside a structure.
Your second array has a fixed size of chars.
Give this a try:
var CharArray10 = ArrayType('char', 10)
var StringArray3 = ArrayType(CharArray10, 3)
var myStructure = Struct({
'ulonglong' : ref.types.ulonglong,
'charptrptr' : StringArray,
'stringArr' : StringArray3
});
More examples here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.