Convert Vector{String} to Ptr{Ptr{UInt8}}?

Probably you want something like:

function stuff(strings::AbstractVector{String})
    GC.@preserve strings begin
        ptrs = Cstring[Base.unsafe_convert(Cstring, s) for s in strings] # array of pointers to char
        @ccall stuff(ptrs::Ptr{Cstring})::Cvoid # or whatever your type signature is
    end
end

So that you allocate an array of Cstring (equivalent to an array of char*, and ensured to be NUL-terminated for C string compatibility) to pass to your C function (Ptr{Cstring} acts like char**). GC.@preserve is used to ensure that the strings array is not garbage collected (is “rooted”) while the ccall executes.

This is usually not a concern. Unicode can still be passed as a char* (e.g. as data for std::string), it is just UTF-8 encoded. To the extent that your application actually cares about the contents of the strings (as opposed to just treating strings as atoms, e.g. filenames), of course, it may need to be UTF8-aware (but not always, e.g. if it is just looking for ASCII substrings).

You usually need some kind of C wrapper for this, either written manually or ala CxxWrap.jl.

1 Like