Manual Indexing into an Allocation

This change should work as expected:

@memcpy(storage[2 * i..].ptr, examples[i]);
zig build run
aabbccddee

From the language reference on memcpy:

dest must be a mutable slice, a mutable pointer to an array, or a mutable many-item pointer

source must be a slice, a pointer to an array, or a many-item pointer

… at least one of source and dest must provide a length, and if two lengths are provided, they must be equal

Your example’s mistakes are that dest is a single-item pointer, and that src is a pointer to a slice, not the slice itself.

In my example (noting there are many other ways to make it work), dest provides a many-item pointer to the target destination, and source is a slice containing the length so that memcpy knows how many items need to be copied.


Edit: fix some URLs

2 Likes