mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
19 lines
479 B
Zig
19 lines
479 B
Zig
const expect = @import("std").testing.expect;
|
|
|
|
test "optional pointers" {
|
|
// Pointers cannot be null. If you want a null pointer, use the optional
|
|
// prefix `?` to make the pointer type optional.
|
|
var ptr: ?*i32 = null;
|
|
|
|
var x: i32 = 1;
|
|
ptr = &x;
|
|
|
|
try expect(ptr.?.* == 1);
|
|
|
|
// Optional pointers are the same size as normal pointers, because pointer
|
|
// value 0 is used as the null value.
|
|
try expect(@sizeOf(?*i32) == @sizeOf(*i32));
|
|
}
|
|
|
|
// test
|