Casting an array of char to float

Why this compiles:

    const from align(@alignOf(f32)) = [_]u8{ 1, 2, 3, 4 };
    const to: f32 = @bitCast(from); // [4]u8

But this does not?

    const from align(@alignOf(f32)) = [_]u8{ 1, 2, 3, 4 };
    const to: f32 = @bitCast(from[0..4]); // *align(4) const [4]u8

with

error: cannot @bitCast from '*align(4) const [4]u8'

Ok I tried to simplify my problem and ended up in a X/Y situation.

Here is what you are supposed to do here:

    const from align(@alignOf(f32)) = [_]u8{ 1, 2, 3, 4 };
    const to: f32 = @bitCast(from[0..4].*);

I’ll keep it here for reference.

1 Like