Hi,
I am trying to pass a two dimensional array [140][140]u8 into a function that accepts a two dimensional [][]u8slice. This does not work. But it works if I change the function to accept a slice of arrays [][140]u8slice. I would like my function to not be dependent on the size.
Sorry if this is a trivial question (I did not manage to find an answer online) but I would be grateful to get an explanation for this behavior. It seems strange to me that the conversion does not work for multidimensional arrays/slices since all the array sizes are available at the compile time.
var input: [140][140]u8 = undefined;
fn modifyTwoDimSlice(in: [][]u8) void {
// fn modifyTwoDimSlice(in: [][140]u8) void { // ← this works
in[0][1] = 255;
}
pub fn main() !void {
modifyTwoDimSlice(&input);
}
Gives the error:
-----------------------
two_dim_slice_question.zig:8:23: error: expected type '[][]u8', found '*[140][140]u8'
modifyTwoDimSlice(&input);
^~~~~~
two_dim_slice_question.zig:8:23: note: pointer type child '[140]u8' cannot cast into pointer type child '[]u8'
two_dim_slice_question.zig:3:26: note: parameter type declared here
fn modifyTwoDimSlice(in: [][]u8) void {
^~~~~~
-----------------------