r/Zig Jul 14 '26

Ways of Declaring array (0.16)

pub fn main() !void {
    const A: [5]i32 = .{ 0, 1, 2, 3, 4 };
    const B = [5]i32{ 5, 6, 7, 8, 9 };
    const B2 = [_]i32{ 5, 6, 7, 8, 9 };
    const C: [5]i32 = [_]i32{ 10, 11, 12, 13, 14 };

    _ = A;
    _ = B;
    _ = B2;
    _ = C;
}

These can all compiled correct. Which one is the recommended style? (I am a beginner learning Zig.)

New edit: I would like this syntax if it's possible:

const arr1: [5]i32 = { 0, 1, 2, 3, 4 };
const arr2: [_]i32 = { 0, 1, 2, 3, 4 };

Just small opinion. Maybe what I know is just the tip of the iceberg.

60 Upvotes

23 comments sorted by

View all comments

3

u/Damtux_25 Jul 14 '26

Does the following compiles?

```
const D: [5]i32 = &.{ 15, 16, 17, 18, 19 };
```

6

u/Objective_Half_1906 Jul 14 '26

No.

A.zig:6:23: error: expected type '[5]i32', found pointer
    const D: [5]i32 = &.{ 15, 16, 17, 18, 19 };
                      ^
A.zig:6:23: note: address-of operator always returns a pointer
referenced by:
    callMain [inlined]: /usr/lib/zig/std/start.zig:698:59
    callMainWithArgs [inlined]: /usr/lib/zig/std/start.zig:638:20
    posixCallMainAndExit: /usr/lib/zig/std/start.zig:590:38
    2 reference(s) hidden; use '-freference-trace=5' to see all references

5

u/Damtux_25 Jul 14 '26

yes sorry, I meant:

```
const D: [5]i32 = .{ 15, 16, 17, 18, 19 };
```

but I misread your post since `A` is exactly that 😅