Bridging with C code

When bridging with C and passing String to C functions expecting const char* or pointers - are wrapper functions like withCString or withUnsafe(Mutable)Pointer(to:) strictly necessary? I've found conflicting information when googling. On the other hand, compiler happily accepts the parameters as they are and everything seems to work.
An explanation on C interop or a pointer (pun intended) to detailed (and up to-date) documentation would be greatly appreciated.

Thanks.

1 Like

This article covers the implicit conversion to pointer types:

2 Likes

When are functions like withUnsafe... and methods like with withCString should be used
then?

The implicit pointer is only valid for the duration of the function call you pass it to. So if you need it to last longer than that you have to use the explicit withUnsafePointer (or, soon, Span) APIs.

3 Likes

Great explanation. Thanks.