#Getting a u8 slice (`[]u8`) from a string?

1 messages · Page 1 of 1 (latest)

unreal dew
#

I want to fix this error, basically:

src/cpu.zig:436:24: error: expected type '[]u8', found '*const [10:0]u8'
    const name: []u8 = "lda (71),Y";
                       ^~~~~~~~~~~~
strong burrow
#

if you need to mutate the string afterwards you need a place for it to live, since slices are pointers and dont hold data. easiest way is just doing this:

var name_arr = “lda (71),Y”.*;
const name: []u8 = &name_arr;

if you dont need it to be mutable, then you can just make this change name: []u8 => name: []const u8

#

the error is about trying to get a mutable pointer from a const pointer

unreal dew
#

const name: [] const u8 is this valid zig tho?

#

Yeah doesn't seem to be

src/cpu.zig:436:17: error: expected type expression, found 'const'
    const name: const []u8 = "lda (71),Y";
                ^~~~~
strong burrow
#

yes, const before the variable name means it cant be reassigned and the const after the [] means the contents cant be changed

unreal dew
#

Oh nevermind

#

I miread your message

#

my bad

strong burrow
#

np, its the same as *const T

unreal dew
#

gotcha, thanks!

#

I didn't know one could do "foo".*

strong burrow
#

its like a shortcut for turning the string literal pointer to an array on the stack

#

since its just a pointer