#how would i get a string as a function parameter
1 messages · Page 1 of 1 (latest)
fn replace(string: *const []u8, to_be_replaced: u8, replace_with: u8) *const []u8 {
var result: *const []u8 = string;
for (result, 0..result.len) |char, i| {
if (char == to_be_replaced) {
result[i] = replace_with;
}
}
return result;
}```
error: src/main.zig:37:45: error: expected type '*const []u8', found '*const [17:0]u8'
I think you wan []const u8
a *const []u8 is a double pointer - are you sure this is what you want?
[]const u8 makes it that result[i] = replace_with; results in an error
*const [17:0]u8 is a pointer to a array of u8. []const u8 (or []u8) are more or less structs, that have a pointer and a len field
there's std.mem.replace, std.mem.replaceOwned, and std.mem.replaceScalar - perhaps you can already use one of them?
even if you want to try to implement the function yourself, it might be beneficial to look at their signatures, to see how they handle things
ok, then use []u8. But know, that you won't be able to pass things with type *const [17:0]u8, because *const [17:0]u8 are immutable and []u8 are not. But use std functions, if possible.
ok