50 lines
1.3 KiB
Zig
50 lines
1.3 KiB
Zig
const expect = @import("std").testing.expect;
|
|
const print = @import("std").debug.print;
|
|
|
|
// Normal pointers in Zig cannot have 0 or null as a value. They follow the syntax *T, where T is the child type.
|
|
// Referencing is done with &variable, and dereferencing is done with variable.*.
|
|
fn increment(num: *u8) void {
|
|
num.* += 1;
|
|
}
|
|
|
|
test "pointers" {
|
|
var x: u8 = 1;
|
|
increment(&x);
|
|
try expect(x == 2);
|
|
}
|
|
|
|
// Trying to set a *T to the value 0 is detectable illegal behaviour.
|
|
test "naughty pointer" {
|
|
var x: u16 = 5;
|
|
x -= 5;
|
|
var y: *u8 = @ptrFromInt(x);
|
|
y = y;
|
|
}
|
|
|
|
// Test [23/126] test.naughty pointer... thread 21598 panic: cast causes pointer to be null
|
|
// ./test-c1.zig:252:18: 0x260a91 in test.naughty pointer (test)
|
|
// var y: *u8 = @ptrFromInt(x);
|
|
// ^
|
|
|
|
// Zig also has const pointers, which cannot be used to modify the referenced data.
|
|
// Referencing a const variable will yield a const pointer.
|
|
|
|
test "const pointers" {
|
|
// const x: u8 = 1;
|
|
// var y = &x;
|
|
// y.* += 1;
|
|
}
|
|
|
|
// error: cannot assign to constant
|
|
// y.* += 1;
|
|
// ^
|
|
// A *T coerces to a *const T.
|
|
|
|
// Pointer Sized Integers
|
|
// `usize` and `isize` are given as unsigned and signed integers which are the same size as pointers.
|
|
|
|
test "usize" {
|
|
try expect(@sizeOf(usize) == @sizeOf(*u8));
|
|
try expect(@sizeOf(isize) == @sizeOf(*u8));
|
|
}
|