I'm trying to interact with an old C terminal app/library from Swift. I've successfully integrated the source code and bridged the headers from C to Swift. The code compiles and runs, I can access all functions from C - library into swift.
There is a structure in C - library which I need to initialize[Function already exists which takes pointer] and assign values to structure variables[Manually].
C-structure:
Struct args{
char ** var1;
unsigned char * var2;
char * var3;
}
and Initialization function call:
init(args * ptr);
How to call the function inside swift and assign values to var1 and var2?
1.Will following snippet successfully initialize the structure?
let Ptr = UnsafeMutablePointer<args>.allocate(capacity: 1)
var args = args()
Ptr.pointee = args
init(Ptr)
2.How to assign values to var1, var2 & var3 assuming we successfully initialize?
They are mapped as:
var1: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!
var2: UnsafeMutablePointer<Uint8>!
var3: UnsafeMutablePointer<Int8>!
For example var1 = {"a", "b"}, var2 = {1,2,3} and var3 = "a"
I've tested following links and did not work:
How to pass an array of Swift strings to a C function taking a char ** parameter : gives 'inout[UnsafeMutablePointer?] to type UnsafeMutablePointer?>!' error
Convert a Swift Array of String to a to a C string array pointer : gives 'inout[UnsafeMutablePointer?] to type UnsafeMutablePointer?>!' error
No built-in support for arrays of C strings : this one needs more efforts and hoping to get easier version
github - Swift wrappers for C functions taking char** arguments : gives 'inout[UnsafeMutablePointer] to type UnsafeMutablePointer?>!' error