Kotlin/Native: How to convert cArrayPointer to Array

724 views Asked by At

How can I convert cArrayPointer to a simple Array/List when using c-interop?

val myArray: Array<Int> = memScoped {
    val cArray = allocArray<IntVar>(5)
    fill(cArray)
    cArray.toSimpleArray()     <--- There is no such function
}
1

There are 1 answers

0
Artyom Degtyarev On

I'd recommend to make it somehow like this:

val myArray: Array<Int> = memScoped {
        val length = 5      //cause I don't know how to get C-array size
        val cArray = allocArray<IntVar>(length)
        (0 until length).map { cArray[it] }.toTypedArray()
}

As one can see in the documentation, CArrayPointer is nothing but a typealias of CPointer. So, I suppose there can't be anadditional functionality, like one you desire.