#can i convert []const u8 to [:0]const u8

1 messages · Page 1 of 1 (latest)

flint plaza
sinful snow
#

Where does the []const u8 you're trying to use come from?

flint plaza
#

from a different part of my program

idle frigate
#

a string literal is a null-terminated string

flint plaza
#

i created a app-like class and now im trying to add visuals with raylib

idle frigate
#

you could use a string literal

flint plaza
#

what do you mean?

sinful snow
idle frigate
#

const hello_world = "hello world";
do you know what this is?

flint plaza
#

i should just change my type in my program?

#

comptime string?

#

im guessing

idle frigate
#

a string literal, which is null terminated

#

you can use string literals

flint plaza
#

why default to null terminated?? doesnt zig avoid that

idle frigate
#

not for literals

flint plaza
#

isnt [:0] only null terminated

idle frigate
#

i use raylib myself, it works

sinful snow
#

String literals are constant single-item Pointers to null-terminated byte arrays. The type of string literals encodes both the length, and the fact that they are null-terminated, and thus they can be coerced to both Slices and Null-Terminated Pointers. Dereferencing string literals converts them to Arrays.

flint plaza
#

that somewhat makes sense i guess

sonic plover
flint plaza
#

ah i remeber reading its both null terminated and the length is stored at the same time

#

is this not a string literal? const str: []u8 const = "hello world;

sinful snow
#

It is, but it's being coerced to a u8 slice

#

Just change the type to the one you need and it'll coerce to that instead

flint plaza
#

what type do i use then? its in a struct

#

the struct is also constant#

sinful snow
#

The type that the function you need to pass it to accepts

flint plaza
#

ok i guess im overcomplicating, thanks that worked

sinful snow
#

👍