Zig 字符串文字是指向以 null 结尾的字节数组的单项指针,非常适合用作 C 的char *字符串!
然而,当我尝试使用 Zig 中的这个简单的 C 函数时:
int count_bytes(const char *str) {
int count = 0;
while(str[count]) {
count++;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
齐格呼叫者:
const std = @import("std");
const c = @cImport({
@cInclude("add.c");
});
const testing = std.testing;
test "should be able to count string length" {
try testing.expectEqual(0, c.count_bytes(""));
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
./src/main.zig:16:46: error: expected type '[*c]u8', found '*const [0:0]u8'
try testing.expectEqual(0, c.count_bytes(""));
^
./src/main.zig:16:46: note: cast discards const qualifier
try testing.expectEqual(0, c.count_bytes(""));
^
Run Code Online (Sandbox Code Playgroud)
本文解释了类似情况下的 Zig 字符串文字,但我无法使用该技巧使字符串成为非 const:
test "should be able to count string length" {
var cstr = "".*;
try testing.expectEqual(0, c.count_bytes(&cstr));
}
Run Code Online (Sandbox Code Playgroud)
结果是一个更奇怪的错误:
./src/main.zig:17:45: error: expected type 'comptime_int', found 'c_int'
try testing.expectEqual(0, c.count_bytes(&cstr));
^
Run Code Online (Sandbox Code Playgroud)
我还尝试将字符串转换为C 指针,如 Zig 文档中所示:
test "should be able to count string length" {
var cstr: [*c]u8 = &"".*;
try testing.expectEqual(0, c.count_bytes(cstr));
}
Run Code Online (Sandbox Code Playgroud)
这也给出了一个错误:
./src/main.zig:16:27: error: expected type '[*c]u8', found '*const [0:0]u8'
var cstr: [*c]u8 = &"".*;
^
./src/main.zig:16:27: note: cast discards const qualifier
var cstr: [*c]u8 = &"".*;
^
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
编辑:
我收到的建议不适用于 Zig 0.9,这是我撰写本文时的最新稳定版本。
如果您认为您知道解决方案,请先尝试一下...将 C 文件放在src-c/add.c,将 Zig 文件放在src/main.zig,然后运行以下命令来尝试:
zig test src/main.zig -I src-c
Run Code Online (Sandbox Code Playgroud)
错误:预期类型“comptime_int”,找到“c_int”
由于 zig 当前如何打印错误位置,所以很难看出,但此错误实际上是针对 ExpectEqual 的第二个参数,而不是针对 count_bytes 的参数的错误
./src/main.zig:17:45: error: expected type 'comptime_int', found 'c_int'
try testing.expectEqual(0, c.count_bytes(&cstr));
^
Would be slightly clearer if it was like this:
./src/main.zig:17:45: error: expected type 'comptime_int', found 'c_int'
try testing.expectEqual(0, c.count_bytes(&cstr));
~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
箭头指向(,意味着它旨在覆盖整个调用表达式
此错误是由于推断参数类型的方式造成的std.testing.expectEqual。
ExpectEqual定义为fn expectEqual(a: anytype, b: @TypeOf(a)) void。这意味着 的类型b必须与 的类型相同a,并且不会尝试对等类型解析。
在本例中, 的类型a是comptime_int因为这是 zig 中整数文字的默认类型,然后当第二个参数是无法转换为的运行时整数时会出错comptime_int
为了解决这个问题,您通常需要在使用时显式强制转换expectEqual
try testing.expectEqual(@as(c_int, 0), c.count_bytes(""));
Run Code Online (Sandbox Code Playgroud)
Issue #4437提出了一种可能的解决方案来解决此问题,但尚未做出任何更改。
错误:预期类型“[*c]u8”,找到“*const [0:0]u8”
我无法使用您提供的代码复制此错误 - 头文件是否可能错误地定义count_bytes为int count_bytes(char *str)而不是int count_bytes(const char *str)?