#how would i get a string as a function parameter

1 messages · Page 1 of 1 (latest)

dim turtle
#

im trying to make a replace function, but idk how to get a string as a function parameter

#
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'

candid kindle
#

I think you wan []const u8

royal dirge
#

a *const []u8 is a double pointer - are you sure this is what you want?

dim turtle
#

[]const u8 makes it that result[i] = replace_with; results in an error

candid kindle
#

*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

royal dirge
candid kindle
dim turtle
#

ok