#How to get the offset of a slice from the beginning of the array it is a slice of?

1 messages · Page 1 of 1 (latest)

boreal lagoon
#

If sl is a slice of array ar, how do I get the offset of the slice from the start of the array?

#

Actually, now that I check, ar is itself a slice returned by Allocator.alloc()

#

if I try to do pointer subtraction I get this:

src/identify.zig:93:42: error: incompatible types: '[*]const u8' and '[]u8'
                const offset = block.ptr - file.buf;
                               ~~~~~~~~~~^~~~~~~~~~
src/identify.zig:93:37: note: type '[*]const u8' here
                const offset = block.ptr - file.buf;
                               ~~~~~^~~~
src/identify.zig:93:48: note: type '[]u8' here
                const offset = block.ptr - file.buf;
                                           ~~~~^~~~
exotic ermine
#

you can use pointer subtraction: sl.ptr - ar.ptr

#

use file.buf.ptr on the rhs