0

I need some help on creating an array as a struct field in Zig.

const VarStr = struct {
    buffer: [1000]u8,
    len: u8,
    pub fn init(str: []const u8) VarStr {
        var filled: u8 = 0;
        for (str) |char, ind| {
            .buffer[ind] = char;
            filled = ind + 1;
        }
        while (filled < 999) {
            .buffer[filled] = null;
        }
    }
};

When i compile, it gives me the following error

error: array access of non-array type '@Type(.EnumLiteral)'
            .buffer[ind] = char;
                   ^

Where did I go wrong? please help, thank you.

1 Answer 1

1

.buffer is not valid zig syntax*. Zig does not have any kind of implicit this object in structs, so you have to make one yourself.

const VarStr = struct {
    buffer: [1000]u8,
    len: u8,
    pub fn init(str: []const u8) VarStr {
        var result: VarStr = .{
            .buffer = undefined,
            .len = @intCast(u8, str.len),
        };
        var filled: u8 = 0;
        for (str) |char, ind| {
            result.buffer[ind] = char;
            filled = ind + 1;
        }
        while (filled < 999) {
            result.buffer[filled] = null;
        }
    }
};

This code will now compile, but it's going to go into an infinite loop because filled in the while never changes. If you're trying to copy the first 999 bytes into buffer and fill the rest with zeroes, you might consider:

var result: VarStr = .{
    .buffer = [_]u8{0} ** 1000, // zero-initialize buffer
    .len = std.math.min(str.len, @as(u8, 999)),
};
std.mem.copy(u8, &result.buffer, str[0..std.math.min(str.len, 999)]);

*: .buffer is syntax for a zig enum literal. Enum literals are used as a shorthand for using enum fields. .buffer[index] is meaningless because you cannot index an enum literal, which is what the error means by array access of non-array type '@Type(.EnumLiteral)'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.