ckotlinkotlin-multiplatformkotlin-nativecinterop

How to create CPointer of primitive data type in Kotlin/Native?


Allocating and obtaining a pointer of a struct is quite simple:

memScoped {
    val str: StructType = alloc<StructType>()
    val strPtr: CPointer<StructType> = str.ptr
}

But I'm struggling to allocate or get pointer of the Int, UInt, Long, ULong and other primitive data types.

enter image description here

Neither there is any extension to these types:

val intData = 5
intData.ptr  // <-- no extension like that

Searched a lot, but there seems no documentation for this.

Any help is greatly appreciated :)


Solution

  • Michael is totally correct, Kotlin primitive types are not supposed to work like that. As explained in the documentation, one should use special types representing lvalues in such cases. Something like this should work:

    memScoped {
        val i = alloc<IntVar>()
        i.value = 5
        val p = i.ptr
    }