#error: expected type '[*:0]const u8', found '[]u8'

1 messages · Page 1 of 1 (latest)

prisma ravine
#

I have the following snippet

        var drawText = try std.fmt.allocPrint(std.heap.page_allocator, "Draw: {}", .{draw});
        defer std.heap.page_allocator.free(drawText);
        var xWinText = try std.fmt.allocPrint(std.heap.page_allocator, "X Win: {}", .{xWin});
        defer std.heap.page_allocator.free(xWinText);
        var oWinText = try std.fmt.allocPrint(std.heap.page_allocator, "O Win: {}", .{oWin});
        defer std.heap.page_allocator.free(oWinText);

        raylib.DrawText(drawText, 0, 600, 20, raylib.WHITE);
        raylib.DrawText(xWinText, 240, 600, 20, raylib.RED);
        raylib.DrawText(oWinText, 500, 600, 20, raylib.GREEN);

It throws the following error

src/main.zig:120:25: error: expected type '[*:0]const u8', found '[]u8'
        raylib.DrawText(drawText, 0, 600, 20, raylib.WHITE);
                        ^~~~~~~~
raylib/raylib.zig:4835:12: note: parameter type declared here
    text: [*:0]const u8,

I believe raylib.DrawText want to receive an array of many pointers while I'm providing it with a slice.

I am not sure what to here to make it work.

#

error: expected type '[*:0]const u8', found '[]u8'

cunning shale
#

[*:0]const u8 is a 0-terminated manyptr of constant u8s

#

not an array of manyptrs

#

i think the 0-termination is the main issue here; it's expecting essentially a C-style string where the length isn't known upfront but you do know it ends with a zero

#

whereas the slices you allocated are []u8, so they have a length stored next to the pointer, but there isn't necessarily a zero at the end

#

luckily, there is std.fmt.allocPrintZ which returns a [:0]u8, i.e. it has a length stored and also ends with 0

prisma ravine
#

ah If I understand correctly, then I will not be able to use allocPrint to do this ?

or I can use allocPrint add \0 add the end and retrieve the pointer out of the slice ? (with .ptr ?) not sure if this is possible

#

ah allocPrintZ, noted

#

thanks!

cunning shale
#

np!

#

i just tested briefly and it seems like the whole cast can actually be done implicitly, i didn't need .ptr even

prisma ravine
#

yep seems like it doesn't need .ptr

#

the working code, in case someone needs it

        var drawText = try std.fmt.allocPrintZ(std.heap.page_allocator, "Draw: {}", .{draw});
        defer std.heap.page_allocator.free(drawText);
        var xWinText = try std.fmt.allocPrintZ(std.heap.page_allocator, "X Win: {}", .{xWin});
        defer std.heap.page_allocator.free(xWinText);
        var oWinText = try std.fmt.allocPrintZ(std.heap.page_allocator, "O Win: {}", .{oWin});
        defer std.heap.page_allocator.free(oWinText);

        raylib.DrawText(drawText, 0, 600, 20, raylib.WHITE);
        raylib.DrawText(xWinText, 240, 600, 20, raylib.RED);
        raylib.DrawText(oWinText, 500, 600, 20, raylib.GREEN);
#

zig version: 0.11.0-dev.4404+4f6013bf5

cunning shale
#

btw, i'd strongly advise against using std.heap.page_allocator directly

#

it's slow since every alloc is a syscall and it wastes memory since every alloc is rounded up to the page size (usually 4096 bytes)

#

you could use an arena on top of page allocator, or even a fixed buffer allocator on top of some stack space, if all you want is to allocate these strings briefly and then free them

crisp topaz
cunning shale
#

right

timid elk
cunning shale
#

yes

#

also since you're using raylib, i think you must have libc linked already, so you could do what i've done in recent projects and use GPA for debug builds and C allocator for release

cunning shale
prisma ravine
#

Ah noted, I thought it should be possible to allocate it on stack somehow since the string size is limited but was unsure how to do it

anwyay here is the bufPrintZ version of it

        var drawTextBuf = [_]u8{0} ** 50;
        var xWinTextBuf = [_]u8{0} ** 50;
        var oWinTextBuf = [_]u8{0} ** 50;
        var drawText = try std.fmt.bufPrintZ(&drawTextBuf, "Draw: {}", .{draw});
        var xWinText = try std.fmt.bufPrintZ(&xWinTextBuf, "X Win: {}", .{xWin});
        var oWinText = try std.fmt.bufPrintZ(&oWinTextBuf, "O Win: {}", .{oWin});

        raylib.DrawText(drawText, 0, 600, 20, raylib.WHITE);
        raylib.DrawText(xWinText, 240, 600, 20, raylib.RED);
        raylib.DrawText(oWinText, 500, 600, 20, raylib.GREEN);

not sure whether I did the array initialization right, but it seems to work

cunning shale
#

that works fine

#

you could also use var buf = std.mem.zeroes([50]u8) or var buf: [50]u8 = undefined, the latter is okay as bufPrintZ will write to the part of the buffer that actually gets used

prisma ravine
#

noted! thanks all!

cunning shale
timid elk
pallid dirge