How to use void **

how to use void ** in rust.
i know

*mut libc::c_void 

is like void *
but how about void **?

*mut *mut std::ffi::c_void

or

let x: *mut std::ffi::c_void = std::ptr::null_mut();
some_ffi_func(&mut x);

when using *mut *mut std::ffi::c_void how to initialize it ??

std::ptr::null_mut() will initialize it with a null value. MaybeUninit is very useful in situations where ffi is needed as well.

Could you provide more context about the problem?

1 Like

Though usually when you need a void**, you are expected to initialize it by taking the address of a void* variable, as in the last example above. This void* doesn't need to point at anything, so it can be initialized to null, as in the example. The function that you pass its address to is supposed to initialize it.

Here's a version with more of the types spelled out.

use std::ffi::c_void;
use std::ptr::null_mut;

extern "C" {
    fn some_ffi_func(arg: *mut *mut c_void);
    fn more_ffi_stuff(ptr: *mut c_void);
}

let mut ptr: *mut c_void = null_mut(); // first pointer
let tmp: *mut *mut c_void = &mut ptr;  // second pointer points to first pointer
some_ffi_func(tmp);                    // uses `tmp` to write to `ptr`

// now `ptr` is initialized and you can do other stuff with it.
more_ffi_stuff(ptr);
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.