Skip to content

Instantly share code, notes, and snippets.

@42LM
Last active February 24, 2026 08:59
Show Gist options
  • Select an option

  • Save 42LM/9ac74b3ca4545ee198a897f67c4c4f75 to your computer and use it in GitHub Desktop.

Select an option

Save 42LM/9ac74b3ca4545ee198a897f67c4c4f75 to your computer and use it in GitHub Desktop.
Zig ⚡️ - Examples for Type/pointer cheatsheet (https://zig.news/toxi/typepointer-cheatsheet-3ne2) [Zig]
// https://zig.news/toxi/typepointer-cheatsheet-3ne2
//
// Examples on how to create/init every type that is part of the type/pointers cheatsheet.
const std = @import("std");
const print = std.debug.print;
/// Cheatsheet stores a list of all types and pointers.
const Cheatsheet = struct {
/// single u8 value
a: u8,
/// single optional u8 value
b: ?u8,
/// array of 2 u8 values
c: [2]u8,
/// zero-terminated array of 2 u8 values
d: [2:0]u8,
// slice of u8 values
e: []u8,
/// slice of optional u8 values
f: []?u8,
/// optional slice of u8 values
g: ?[]u8,
/// pointer to u8 value
h: *u8,
/// pointer to optional u8 value
i: *?u8,
/// optional pointer to u8 value
j: ?*u8,
/// pointer to immutable u8 value
k: *const u8,
/// pointer to immutable optional u8 value
l: *const ?u8,
/// optional pointer to immutable u8 value
m: ?*const u8,
/// pointer to multiple u8 values
n: [*]u8,
/// pointer to multiple zero-terminated u8 values
o: [*:0]u8,
/// array of 2 u8 pointers
p: [2]*u8,
/// pointer to array of 2 u8 values
q: *[2]u8,
/// pointer to zero-terminated array of 2 u8 values
r: *[2:0]u8,
/// pointer to immutable array of 2 u8 values
s: *const [2]u8,
/// pointer to slice of immutable u8 values
t: *[]const u8,
/// slice of pointers to u8 values
u: []*u8,
/// slice of pointers to immutable u8 values
v: []*const u8,
/// pointer to slice of pointers to immutable optional u8 values
w: *[]*const ?u8,
};
pub fn main() !void {
// cheatsheet depends on
var ptr: u8 = 244;
var ptr_optional: ?u8 = null;
const ptr_unmut: u8 = '8';
const ptr_unmut_optional: ?u8 = null;
var array = [_]u8{ 'w', 'o' };
var array_zero_terminated = [_:0]u8{ 'w', 'o' };
// create the cheatsheet
var ch = Cheatsheet{
.a = '7',
.b = null,
.c = undefined,
.d = undefined,
.e = &[_]u8{},
.f = &[_]?u8{},
.g = null,
.h = &ptr,
.i = &ptr_optional,
.j = null,
.k = &ptr_unmut,
.l = &ptr_unmut_optional,
.m = null,
.n = undefined,
.o = undefined,
.p = undefined,
.q = &array,
.r = &array_zero_terminated,
.s = array[0..],
.t = undefined,
.u = undefined,
.v = undefined,
.w = undefined,
};
print("Cheatsheet:\n\n", .{});
// u8
// single u8 value
formatPrint(ch.a, "*");
// ?u8
// single optional u8 value
formatPrint(ch.b, "*");
ch.b = '1';
formatPrint(ch.b, "");
// [2]u8
// array of 2 u8 values
formatPrint(ch.c, "*");
ch.c = [2]u8{ '{', '}' };
formatPrint(ch.c, "");
ch.c[0] = '(';
formatPrint(ch.c, "");
// [2:0]u8
// zero-terminated array of 2 u8 values
formatPrint(ch.d, "*");
ch.d = [2:0]u8{ '*', '*' };
formatPrint(ch.d, "");
// []u8
// slice of u8 values
formatPrint(ch.e, "*");
var tmp_array = [_]u8{ 1, 2, 3 };
ch.e = tmp_array[0..];
formatPrint(ch.e, "");
formatPrint(ch.e, "");
// []?u8
// slice of optional u8 values
formatPrint(ch.f, "*");
// const optinal_one: ?u8 = 1;
// const optinal_two: ?u8 = 2;
// var tmp_array2 = [_]?u8{ optinal_one, optinal_two };
var tmp_array2 = [_]?u8{ 1, 2 };
ch.f = tmp_array2[0..];
formatPrint(ch.f, "");
// ?[]u8
// optional slice of u8 values
formatPrint(ch.g, "*");
var tmp_array3 = [_]u8{ 1, 2, 3 };
ch.g = tmp_array3[0..];
formatPrint(ch.g, "");
// *u8
// pointer to u8 value
formatPrint(ch.h, "*");
// *?u8
// pointer to optional u8 value
formatPrint(ch.i, "*");
formatPrint(ch.i.*, "");
ptr_optional = 7;
formatPrint(ch.i.*, "");
// ?*u8
// optional pointer to u8 value
formatPrint(ch.j, "*");
var ptr2: u8 = 7;
ch.j = &ptr2;
formatPrint(ch.j, "");
// *const u8
// pointer to immutable u8 value
formatPrint(ch.k, "*");
// *const ?u8
// pointer to immutable optional u8 value
formatPrint(ch.l, "*");
// ?*const u8
// optional pointer to immutable u8 value
formatPrint(ch.m, "*");
ch.m = &ptr_unmut;
formatPrint(ch.m, "");
// [*]u8
// pointer to multiple u8 values
formatPrint(ch.n, "*");
var tmp_array4 = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
const slice = tmp_array4[0..];
const ptr_to_u8: [*]u8 = slice.ptr;
ch.n = ptr_to_u8;
formatPrint(ch.n, "");
formatPrint(ch.n[0], "");
formatPrint(ch.n[1], "");
formatPrint(ch.n[2], "");
formatPrint(ch.n[3], "");
formatPrint(ch.n[4], "");
// [*:0]u8
// pointer to multiple zero-terminated u8 values
formatPrint(ch.o, "*");
var tmp_array5 = [_:0]u8{ 'm', 'u', 't' };
const slice2 = tmp_array5[0..];
// const c_string: [*:0]u8 = slice2.ptr;
const c_string = slice2.ptr;
ch.o = c_string;
formatPrint(ch.o, "");
formatPrint(ch.o[0], "");
formatPrint(ch.o[1], "");
formatPrint(ch.o[2], "");
// [2]*u8
// array of 2 u8 pointers
formatPrint(ch.p, "*");
var tmp_uno: u8 = 'm';
var tmp_dos: u8 = 'u';
const tmp_array6 = [2]*u8{ &tmp_uno, &tmp_dos };
ch.p = tmp_array6;
formatPrint(ch.p, "");
formatPrint(ch.p[0].*, "");
formatPrint(ch.p[1].*, "");
// *[2]u8
// pointer to array of 2 u8 values
formatPrint(ch.q, "*");
formatPrint(ch.q[0], "");
formatPrint(ch.q[1], "");
ch.q[0] = 'X';
ch.q[1] = 'X';
formatPrint(ch.q[0], "");
formatPrint(ch.q[1], "");
var tmp_array7 = [2]u8{ 'a', 'b' };
const slice3 = tmp_array7[0..];
ch.q = slice3;
formatPrint(ch.q, "");
formatPrint(ch.q[0], "");
formatPrint(ch.q[1], "");
// *[2:0]u8
// pointer to zero-terminated array of 2 u8 values
formatPrint(ch.r, "*");
// *const [2]u8
// pointer to immutable array of 2 u8 values
formatPrint(ch.s, "*");
formatPrint(ch.s[0], "");
formatPrint(ch.s[1], "");
// *[]const u8
// pointer to slice of immutable u8 values
formatPrint(ch.t, "*");
const tmp_array8 = [2]u8{ 'a', 'b' };
var slice4: []const u8 = tmp_array8[0..];
ch.t = &slice4;
formatPrint(ch.t, "");
// []*u8
// slice of pointers to u8 values
formatPrint(ch.u, "*");
var tmp_tres: u8 = 'm';
var tmp_quatro: u8 = 'u';
var tmp_array9 = [_]*u8{ &tmp_tres, &tmp_quatro };
const slice5 = tmp_array9[0..];
ch.u = slice5;
formatPrint(ch.u, "");
// []*const u8
// slice of pointers to immutable u8 values
formatPrint(ch.v, "*");
const a: u8 = 'a';
const b: u8 = 'b';
var tmp_array10 = [_]*const u8{ &a, &b };
const slice6 = tmp_array10[0..];
ch.v = slice6;
formatPrint(ch.v, "");
// *[]*const ?u8
// pointer to slice of pointers to immutable optional u8 values
formatPrint(ch.w, "*");
const a_opt: ?u8 = null;
const b_opt: ?u8 = 'b';
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var list = std.ArrayList(*const ?u8).init(allocator);
errdefer list.deinit();
try list.append(&a_opt);
try list.append(&b_opt);
var woop = try list.toOwnedSlice();
defer allocator.free(woop);
ch.w = &woop;
formatPrint(ch.w, "");
formatPrint(ch.w.*[0].*, "");
formatPrint(ch.w.*[1].*.?, "");
// print the complete cheatsheet struct
// std.debug.print("\nCheatsheet struct: {any}", .{ch});
}
/// formatPrint debug prints a std format for displaying each field of the cheatsheet.
fn formatPrint(value: anytype, prefix: []const u8) void {
const value_type = @TypeOf(value);
print("{s:>2}{:>10}: {any}\n", .{ prefix, value_type, value });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment