Big Chungus
03/28/2023, 7:45 AMchar**
in C is represented as CPointerVar<Char>
in kotlin?vbsteven
03/28/2023, 7:59 AMCPointerVar<ByteVar>
char**
-related conversions.
/**
* Convert a Kotlin [List<String>] into its native equivalent.
*/
public fun List<String>.toCStringList(scope: MemScope): CArrayPointer<CPointerVarOf<CPointer<ByteVar>>> = with(scope) {
return allocArrayOf(this@toCStringList.map { it.cstr.getPointer(this) } + null)
}
/**
* Convert a native char** into a Kotlin [List<String>].
*/
public fun CArrayPointer<CPointerVarOf<CPointer<ByteVar>>>.toKStringList(): List<String> = buildList {
var i = 0
while (this@toKStringList[i] != null) {
add(this@toKStringList[i]!!.toKString())
i++
}
}
PoC code, this won't work for all methods. As sometimes the char**
is null-terminated, and sometimes it has a fixed sizeBig Chungus
03/28/2023, 9:21 AMvbsteven
03/28/2023, 9:40 AM**
for GError out pointers, where you want to pass in a pointer to a GError
pointer, so GTK can fill it with a pointer to the error when it encounters an errorBig Chungus
03/28/2023, 9:42 AMvbsteven
03/28/2023, 9:44 AMchar**
means a pointer to the first element in an array of char*
char*
means a pointer to the first element in an array of char
, And C-strings are an array of char
terminated by \0
Big Chungus
03/28/2023, 9:47 AMvbsteven
03/28/2023, 9:48 AMchar**
case for GTK null-terminated string arrays, in memory it will look like [ptr, ptr, ptr, null]
. where each ptr
points to [char, char, char, \0]
, something like thatBig Chungus
03/28/2023, 9:49 AMvbsteven
03/28/2023, 9:49 AM\0
is a null byte (because char is typically byte). In the case of char**
since it's an array of pointers, the final element is a null
pointerBig Chungus
03/28/2023, 9:50 AMvbsteven
03/28/2023, 9:51 AMtoKstring()
to convert it to a Kotlin String. and you can do myKotlinString.cstr
to get a char*
pointer to the c-String for your kotlin stringBig Chungus
03/28/2023, 9:51 AMLandry Norris
03/28/2023, 2:03 PMchar* strings[size];
, which is explicitly an array. I believe Kotlin will read this as a *uint8_t* *const srcSlice[]
as srcSlice: CValuesRef<CPointerVar<uint8_tVar>>
in Kotlinephemient
03/28/2023, 2:42 PMLandry Norris
03/28/2023, 3:35 PMchar c = (char) NULL
just as easily. In the same way, char** words
is essentially equivalent to char* words[]
. C also lets you do interesting things like 2[words] = "This works"
or *(words + 3) = "This works too"
. C has been around for a long time, so there’s a lot of variations on almost anything. Some lists, like strings, use a null terminator, and others, like the srcSlice I mentioned earlier will require you to calculate the size (the calculation for srcSlice length is not fun) or know it.ephemient
03/28/2023, 4:05 PMLandry Norris
03/28/2023, 4:25 PM