#expected type '[*c]const u8', found '[65]u8'

1 messages · Page 1 of 1 (latest)

sand verge
#

Mh this error is really buggy me today,

So I have a function that do not change the content of the array, so I declare it []const u8 but from
another source I generate a vector that can be modified []u8, and I pass this vector to the function
that do not modify the code,

So something like that

fn foo(arr: *[]const u8) {
  // write down to the wire
}

fn generate_byte(arr: *[]u8) {
  // generate the buffer
}

fn main() {
  var buff = ...;
  generate_byte(buff);
  foo(buff);
}

Can someone help me to clarify this const stuff?

soft jetty
#

Your error message indicates that buff is an array. To pass an array to a function expecting a slice (such as []u8), you can take its address, and it will coerce to a slice: generate_byte(&buff).

Also, in your foo and generate_byte functions, you probably don't want *[]const u8 and *[]u8, but just []const u8 and []u8. Slices ([]) are already a type of pointer (a slice is a pointer with an associated length)

sand verge
#

Mh Mh!

#

I think I should it with a fresh mind haha

#

I should write allo this kind of pointer operation down

#

thanks for the answer 🙂

soft jetty
sand verge
#

Thanks!

#

I missed it

#

this document is really one shot and my screen reader get crazy with it

soft jetty
#

Ah, yeah, it is pretty large 😅 Might be a little easier to navigate if there were a version with one page per section (the GNU manuals have an option for this, which is a nice idea)

sand verge
#

yeah the GNU manuals are pretty accessibe

#

in fact I donate to the gnu foundation just for this

#

and for emacs ofc 🤣

sand verge
#

Ok I think I do not get also this expected type '[]const u8', found '[*]u8'