mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 23:52:31 +00:00
96164ce613
add pointer arithmetic for unknown length pointer
45 lines
790 B
Zig
45 lines
790 B
Zig
const std = @import("std");
|
|
const assert = std.debug.assert;
|
|
|
|
test "dereference pointer" {
|
|
comptime testDerefPtr();
|
|
testDerefPtr();
|
|
}
|
|
|
|
fn testDerefPtr() void {
|
|
var x: i32 = 1234;
|
|
var y = &x;
|
|
y.* += 1;
|
|
assert(x == 1235);
|
|
}
|
|
|
|
test "pointer arithmetic" {
|
|
var ptr = c"abcd";
|
|
|
|
assert(ptr[0] == 'a');
|
|
ptr += 1;
|
|
assert(ptr[0] == 'b');
|
|
ptr += 1;
|
|
assert(ptr[0] == 'c');
|
|
ptr += 1;
|
|
assert(ptr[0] == 'd');
|
|
ptr += 1;
|
|
assert(ptr[0] == 0);
|
|
ptr -= 1;
|
|
assert(ptr[0] == 'd');
|
|
ptr -= 1;
|
|
assert(ptr[0] == 'c');
|
|
ptr -= 1;
|
|
assert(ptr[0] == 'b');
|
|
ptr -= 1;
|
|
assert(ptr[0] == 'a');
|
|
}
|
|
|
|
test "double pointer parsing" {
|
|
comptime assert(PtrOf(PtrOf(i32)) == **i32);
|
|
}
|
|
|
|
fn PtrOf(comptime T: type) type {
|
|
return *T;
|
|
}
|