Passing `&[&CStr]` to a function taking `*const *const c_char`

I have a binding to a C function with a signature like this (that expects an array of count pointers to null-terminated strings).

fn foo(count: usize, strings: *const *const c_char);

If I have a value strings: &[&CStr] is it ok to pass it like this?

foo(strings.len(), strings.as_ptr() as _);

Actually, I think I have already figured out that the answer is no, the source code for CStr has the following comment.

    // FIXME: this should not be represented with a DST slice but rather with
    //        just a raw `c_char` along with some form of marker to make
    //        this an unsized type. Essentially `sizeof(&CStr)` should be the
    //        same as `sizeof(&c_char)` but `CStr` should be an unsized type.

If I understand correctly, this means that &CStr is currently a wide pointer, so the cast I was suggesting will not work. Is that right? If this FIXME is implemented, would it make my cast OK?

No, it is not – CStr is a DST similar to a str, and so is a regular slice. It's already wrong to pass a reference to a slice where a pointer to an element is expected.

OK, got it, thank you. I just thought that CStr might have some special exemption here and the comment that I cited suggests that maybe something like that is planned.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.