I am learn learning recursion and am trying to do a practice problem, but I’m having an issue with passing a 2d bool array as a function parameter.
The error:
path_finding.zig:18:78: error: expected type [ ][ ]bool’, found ‘*[3][7]bool’
const path_found:bool = walk(&maze, ‘#’, current_location, end_location, &visited_coordinate, &path, path_index);
^~~~~~~~~~~~~~~~~~~
path_finding.zig:18:78: note: pointer type child ‘[7]bool’ cannot cast into pointer type child ‘[ ]bool’
path_finding.zig:24:81: note: parameter type declared here
fn walk(maze:[ ]const[ ]const u8, wall:u8, cur_point:Point, end_point:Point, seen:[ ][ ]bool, path:[
]Point, path_index:u8) bool {
what I don’t understand is why passing visited_coordinate as a parameter causes this issue while maze passes just fine. When I remove & from the function call, I get a “variable never mutated” error. What do I need to do to make this work?
const std = @import("std");
const Point = struct {
x:u8,
y:u8,
};
pub fn main() void {
const maze = [_][]const u8{"#####E#",
"# #",
"#S#####",
};
var visited_coordinate:[3][7]bool = .{.{false} ** 7} ** 3;
const current_location:Point = .{.x = 1, .y = 2};
const end_location:Point = .{.x = 5, .y = 0};
var path:[7]Point = undefined;
const path_index = 0;
const path_found:bool = walk(&maze, '#', current_location, end_location, &visited_coordinate, &path, path_index);
if (path_found == true) {
std.debug.print("{any}\n", .{path});
}
}
fn walk(maze:[]const[]const u8, wall:u8, cur_point:Point, end_point:Point, seen:[][]bool, path:[]Point, path_index:u8) bool {
_ = maze;
_ = wall;
_ = cur_point;
_ = end_point;
_ = seen;
_ = path;
_ = path_index;
//code to be written
}