I've seen the type void[]
used in a few places in the D documentation and function signatures. What does it mean?
I've searched the array section in the documentation as well as Google, but didn't turn up anything conclusive. All I found is that all arrays are implicitly convertible to the void[]
type. Is it just an untyped array?
void (C++) If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword. A void* pointer can't be dereferenced unless it's cast to another type. A void* pointer can be converted into any other type of data pointer.
A function that does not return is treated as a function that returns a void value. A non-returning function can be assigned to a variable of type void . Initializing a variable without a specified type with a function that doesn't return will infer the variable type to be void .
You can't have a variable of type void (or an array of void ) for the simple reason that there are no values of type void for it to hold.
In C, it is possible to have array of all types except following. 1) void. 2) functions. But we can have array of void pointers and function pointers.
void[]
is just an array of anything. in void[]
is a convenient signature for a function that just writes bytes to a file or something because it will accept any kind of array - void
meaning the type isn't important and in
meaning you aren't meaning to modify or store it, thus allowing you to accept const or immutable arrays too.
To use a void[]
, you must first cast it to something else, typically, ubyte[]
, so the elements have a concrete size allowing you to index it. void[].length
is given as the total number of bytes in the array, so if you are just passing a pointer and length of data to a function (like an operating system level file write, e.g. Linux's write
syscall), passing arr.ptr, arr.length
will get those bytes written without caring about what they represent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With