0

Let's say we have the following C function prototype:

int do_something(char* buffer, int buff_size);

How do I pass a zig u8 buffer to this function? Say,

var buffer: [64]u8 = undefined;

_ = c_lib.do_something(buffer.ptr, 64);

I'm getting the error error: no member named 'ptr' in '[64]u8'.

How do I do this in idiomatic Zig?

c21253
  • 330
  • 4
  • 13

1 Answers1

2

For reference, this C function after being translated to Zig will look similar to this:

fn do_something(arg_buffer: [*c]u8, arg_buff_size: c_int) c_int;

buffer is an array (not a slice) of type *[64]u8.

var buffer: [64]u8 = undefined;

It doesn't have ptr field, but can be type coerced (implicitly cast) to [*c]u8 pointer:

_ = c_lib.do_something(&buffer, buffer.len);

If you were using a slice, the code would've looked like this:

var buffer: []u8 = ...
_ = c_lib.do_something(buffer.ptr, @intCast(c_int, buffer.len));
sigod
  • 3,514
  • 2
  • 21
  • 44