How to copy int array to double CValuesRef

var A = arrayOf(1,2,3,4,5,1,3,5,2,4,1,4,2,5,3);

how to convert this to to C function with below type parameter

external fun testFunction(matrix: CValuesRef?)

I would try using either a UIntArray or DoubleArray

Hello @dickensas! Have you tried A.toCValues()?

tried but I am getting the error as
Type mismatch: inferred type is Array< Int > but Array<CPointer<???>?> was expected

the method definition says
CValuesRef< DoubleVar>?

If I code like this
var a = alloc< DoubleVar>()

then no errors throws, but how to transfer that array to this now?

If I put like this then it accepts without any error
var a = allocArray(15)

this is the code, kind of lengthy but it works

var A = arrayOf(1,2,3,4,5,1,3,5,2,4,1,4,2,5,3)
var a = allocArray(A.size)
for (i in 0 until A.size)
a[i] = A[i].toDouble()


then the array is properly affected by the C function pointer

Well, this should work. I also would like to recommend some other approach, just to avoid native memory allocation. Please try this one:

val A = listOf(1,2,3,4,5,1,3,5,2,4,1,4,2,5,3).map { it.toDouble() }.toDoubleArray()

actually the testFunction() is a C function interoped from a predefined static library

int testFunction(double* b)

which is interoped to and the outcome is coming via the parameter as reference

testFunction(a: CValuesRef?) : Int

I tried to call with the A.toCValues() , after the method call the array remains the same without any changes

actually it should be transformed like matrix manipulation

1 Like