Skip to content

Conversation

@darthLeviN
Copy link

*[N]T can be dereferenced. Example :

test "dereferencing *[N]T works" {
    var arr: [3]u8 = .{ 1, 2, 3 };
    const ptr: *[3]u8 = &arr; // const, since we never reassign ptr

    // Dereference to get a copy
    const copy: [3]u8 = ptr.*;
    try std.testing.expectEqual(@as(u8, 1), copy[0]);
    try std.testing.expectEqual(@as(u8, 2), copy[1]);
    try std.testing.expectEqual(@as(u8, 3), copy[2]);

    // Mutate through pointer
    ptr[1] = 42;
    ptr.*[0] = 0;
    try std.testing.expectEqual(@as(u8, 42), arr[1]);
    try std.testing.expectEqual(@as(u8, 0), arr[0]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant