Not for a fixed length array as you’ve written but for a slice you totally can.
var s:[]u8 = undefined;
s.ptr = some_c_ptr;
s.len = some_len;
To be honest though its much easier in most cases I’ve found to just construct a new slice with the values you want off of another pointer. which you’re allowed to do if they’re multi-pointer [*].
var my_array_ptr: [*]u8 = ...;
var s = my_array_ptr[0..some_len];
and there is no safety checking on this. just bounds checking on the slices after you’ve constructed them
Thanks! I didn’t expect any safety checks for doing this either. And that seems fine.
The reason why I wanted to mutate the original slice is because I’d like the array allocation for a slice to be embedded in a struct, but I’d like to vary its length in the struct’s “constructor”. Storing another slice just to be able to vary the increases memory the struct’s memory usage.
If you do slice.len -= 1 you get a safety check in debug modes. But remember, if it’s possible for underflow to occur, you should handle that in your code, not just let Zig insert a panic and rely on that.