Creating and modifying an array of random floats

bytes is an array, but the function wants a slice (ptr + length). You can coerce an array to a slice with just &bytes.

The next issue is it needs a mutable (non const) slice in order to mutate the data. To do that you just need to make bytes a var instead of const.

since bytes will be overwritten imediately anyway, there is no point filling it with any values. zig lets you set values to undefined to explicitly not initialise them.

Zig should tell you at least the first, but it might not since it sees bytes is const so it couldn’t become a mutable slice.

I really do suggest switching to 0.15.2 if you are this new to zig, master is just in a lot of turmoil and any resources may go out of date in a matter of days.
0.15.2 or any older “stable” release will have much more useful resources, and there will be more people able to help too. Though up-to-date resources will still be rarer than most other languages since zig changes so often.

1 Like