# How to convert from main function args: Array\<String\> to C type main function argc, char \*\*argv?

**URL:** <https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492>\
**Category:** Native\
**Created:** [May 4, 2020, 4:25pm UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492 "2020-05-04T16:25:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![agb1](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agb1](https://discuss.kotlinlang.org/u/agb1)\
**Post date:** [May 4, 2020, 4:25pm UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/1 "2020-05-04T16:25:19Z")

</div>

I am trying to use C GUI library with kotlin/native (win 10 pro / x64). Got some progress, but main initialization function of that toolkit library expect parameters exactly the same as one defined for regular C application main function i.e.: (int argc, char \*\*argv).

How do I convert kotlin main function parameters args: Array to C int argc, char \*\*argv  
equivalent ?

I’ve was unable to find usable solution so far.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 4, 2020, 5:08pm UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/2 "2020-05-04T17:08:04Z")

</div>

I don’t really have much experience with native, but from what i can tell and based on [https://kotlinlang.org/docs/reference/native/c\_interop.html#passing-pointers-to-bindings](https://kotlinlang.org/docs/reference/native/c_interop.html#passing-pointers-to-bindings) I came up with this:

```kotlin
fun main(args: Array<String>) {
    val argc = args.count
    val argv = args.map { 
        it.cstr.getPointer(nativeHeap)
    }.toCValues()
}

```

Then you can just call the c function, assuming you have the necessary bindings already.

---

<div class="post-metadata">

**Author:** ![agb1](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agb1](https://discuss.kotlinlang.org/u/agb1)\
**Post date:** [May 5, 2020, 10:25am UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/3 "2020-05-05T10:25:42Z")

</div>

**Wasabi375** , thank you for suggested code. I’ve tried it, but unfortunatly got following issues:

1. For _args.count_ statement I’ve got error: function invocation ‘count()’ expected

2. For _it.cstr.getPointer(nativeHeap)_ statement: error: type mismatch: inferred type is nativeHeap but AutofreeScope was expected

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 5, 2020, 12:41pm UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/4 "2020-05-05T12:41:18Z")

</div>

> [@agb1](#):
>
> For _args.count_ statement I’ve got error: function invocation ‘count()’ expected

Wooops, I always mix up `size` and `count()`. Both work, they are pretty much the same.

> [@agb1](#):
>
> 1. For _it.cstr.getPointer(nativeHeap)_ statement: error: type mismatch: inferred type is nativeHeap but AutofreeScope was expected

As I said, I don’t have much experience with native and even less with C interop. That said, the code should work if you wrap it in a `memScoped` block.  
[https://kotlinlang.org/docs/reference/native/c\_interop.html#memory-allocation](https://kotlinlang.org/docs/reference/native/c_interop.html#memory-allocation)

---

<div class="post-metadata">

**Author:** ![Artyom.Degtyarev](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Artyom.Degtyarev](https://discuss.kotlinlang.org/u/Artyom.Degtyarev)\
**Post date:** [May 6, 2020, 9:10am UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/5 "2020-05-06T09:10:02Z")

</div>

Hi, @agb1! Unfortunately, the documentation on C Interop can be misleading sometimes, some parts of it are slightly outdated. For now, there are two main ways you can send a string to a C function:

1. allocating the memory inside of a memScoped block, as @Wasabi375 suggested, or
2. instantiating an instance of `Arena` class, using it to allocate memory like this:

```auto
val ar = Arena()
val ref = "Hello".cstr.getPointer(ar)
...
ar.clear() // wipes all the memory allocated on the arena before

```

The main difference here will be the lifetime of the allocated object. The first approach guarantees that the memory won’t be cleaned till the end of the block. So, if you want this pointer to be fine for all program’s lifetime, it can be a bad idea to wrap all `main()` lines into a one big `memScoped` block. On the other side, `memScoped` is a lot easier to use as there is no need to clear it manually. For a quick sending something to the C side, it fits much better IMHO.

---

<div class="post-metadata">

**Author:** ![agb1](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agb1](https://discuss.kotlinlang.org/u/agb1)\
**Post date:** [May 6, 2020, 5:53pm UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/6 "2020-05-06T17:53:53Z")

</div>

Hello @Wasabi375 ! Thanks again for your comments on the subject of this message - they were usefull !

---

<div class="post-metadata">

**Author:** ![agb1](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agb1](https://discuss.kotlinlang.org/u/agb1)\
**Post date:** [May 6, 2020, 5:59pm UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/7 "2020-05-06T17:59:35Z")

</div>

Hi, @Artyom.Degtyarev ! Thank you for providing additional information. I would try both solutions to see what would be the best one to apply in my code. Could you please advice - what’s the best place to look for up to date docs regarding kotlin/native ? Would it be github project page and source code ? Also for cases like one I mentioned it would be greate if some kind on demo source code or just parts of code could be provided (I mean short complete application demonstrating usage and probable with comments regarding what are the requirements and restrictions for that solution).  
Thank you for your suggestion !

---

<div class="post-metadata">

**Author:** ![Artyom.Degtyarev](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Artyom.Degtyarev](https://discuss.kotlinlang.org/u/Artyom.Degtyarev)\
**Post date:** [May 6, 2020, 7:17pm UTC](https://discuss.kotlinlang.org/t/how-to-convert-from-main-function-args-array-string-to-c-type-main-function-argc-char-argv/17492/8 "2020-05-06T19:17:36Z")

</div>

About the documentation - we’re trying to keep the doc up-to-date, this particular mistake should be solved soon. Website [kotlinlang.org](http://kotlinlang.org) is the most reliable source of documentation for now. I would also recommend you to check things on a [stdlib reference](https://kotlinlang.org/api/latest/jvm/stdlib/), it is guaranteed to be up-to-date with the latest release.  
Some useful examples can be found across [K/N GitHub repo](https://github.com/jetbrains/kotlin-native/), in the `/samples/` directory. There is a couple of examples could be interesting for you. Also, there is a [kotlin-libui project](https://github.com/msink/kotlin-libui) with similar goals if I understand you correctly. Maybe looking through it can help you.
