Converting [u8; 16] to *const i8

#1
  1. I get the following error:
   = note: expected type `*const i8`
              found type `&'static [u8; 16]`
  1. This is generated by trying to pass
b"00000000:83:00.0" 

(a PCI bus id) to a C FFI function that expects a char*

  1. How do I fix this?
0 Likes

#2

You will need two casts, one to *const [i8;16] then to *const i8.

1 Like

#3

If my initial string is NOT null termianted, but the C FFI does need it to be NULL termianted, then I actually need CString::new(…) right?

1 Like

#4

If you’re ok with copying the data (you don’t need a pointer to those particular bytes), yes, and you pass the result of as_ptr(). From your use case, this is what you want this time.

If you need that memory specifically to be passed to FFI for some reason (usually &mut and C is going to modify something there) you want &Cstr instead, and you’ll have to put the nul termination first.

1 Like