Zig 错误:非数组类型的数组访问
我需要一些关于在 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;
}
}
};
当我编译时,它给我以下错误
error: array access of non-array type '@Type(.EnumLiteral)'
.buffer[ind] = char;
^
我哪里出错了?请帮忙,谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.buffer
不是有效的 zig 语法*。 Zig 在结构中没有任何类型的隐式this
对象,因此您必须自己创建一个。该代码现在可以编译,但它将进入无限循环,因为 while 中的
filled
永远不会改变。如果您尝试将前 999 个字节复制到buffer
中并用零填充其余部分,您可能会考虑:*:
.buffer
是 zig 枚举文字的语法。枚举文字用作使用枚举字段的简写。.buffer[index]
毫无意义,因为您无法索引枚举文字,这就是非数组类型“@Type(.EnumLiteral)”的数组访问的错误含义
.buffer
is not valid zig syntax*. Zig does not have any kind of implicitthis
object in structs, so you have to make one yourself.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 intobuffer
and fill the rest with zeroes, you might consider:*:
.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 byarray access of non-array type '@Type(.EnumLiteral)'