DEV Community

Cover image for Quick Zig and C String Conversion Conundrums
Joshua Matthews
Joshua Matthews

Posted on

Quick Zig and C String Conversion Conundrums

Intro

My background is mostly in C and as I am still new to zig some of the type conversions needed for C and Zig to talk were not crystal clear at the beginning. Now I understand them and I'll give a quick rundown to hopefully help anyone else that needs it.

C String types

Let's start off with what a C string type is in Zig. There are 2 recommended1 ways of denoting a C string.

// Sentinel slice of unknown amount
[*:0]const u8
// Slice of unknown amount
[*]const u8
Enter fullscreen mode Exit fullscreen mode

If you can expect the string to be null-terminated you want the first option which can be converted into a Zig slice with the std.mem.span function. Otherwise, you'll want the second option with you usually requiring a length parameter passed into your exported function so you can get a slice-by-length.

Examples:

export pub fn test_c_string(str: [*:0]const u8) void {
    const local_slice: []const u8 = std.mem.span(str);
    // rest of function
}
Enter fullscreen mode Exit fullscreen mode
export pub fn test_c_string(str: [*]const u8, len: usize) void {
    const local_slice: []const u8 = str[0..len];
    // rest of the function
}
Enter fullscreen mode Exit fullscreen mode

That's really all you need to know for your C string needs. The rest of Zig's strings can convert between Zig slice/array types fairly easily without much intervention.

One type that confused me at first was array sentinel types (i.e. [5:0]const u8) because I assumed it was similar to [*:0]const u8 but the difference is the comptime length (i.e. 5) which turns this slice into a known length so Zig can do it's slice conversions between similar types easily.

  1. You can also do [*c] to signify a C pointer but it is noted this should only be used in autogenerated code.

Top comments (0)