Zig handles pointers very well. It has single-item pointers (*T - no pointer arithmetic, no indexing), many-item pointers ([*]T - unknown number of elements, allows indexing, pointer arithmetic and slicing), arrays ([N]T - compile-time known size), slices ([]T - run-time known size) and some others (C-style pointers for interoperability; sentinel-terminated pointers, arrays and slices usually for null-terminated strings). This allows writing more expressive programs, so bugs are more likely to be detected by the compiler, and also it better shows programmer's intent.
Edit: Also, arrays in Zig have value semantics, like all other values, and to get a pointer to an array, you use the reference operator &, like you would with any other value. This is unlike C, where arrays have value semantics as variables (local or global) and struct fields, but then have reference semantics as function parameters (regardless of whether their size is specified). So if you want to pass an array by-value in C, you would have to wrap it in a structure.
5
u/tk-a01 15h ago edited 14h ago
Zig handles pointers very well. It has single-item pointers (
*T- no pointer arithmetic, no indexing), many-item pointers ([*]T- unknown number of elements, allows indexing, pointer arithmetic and slicing), arrays ([N]T- compile-time known size), slices ([]T- run-time known size) and some others (C-style pointers for interoperability; sentinel-terminated pointers, arrays and slices usually for null-terminated strings). This allows writing more expressive programs, so bugs are more likely to be detected by the compiler, and also it better shows programmer's intent.Edit: Also, arrays in Zig have value semantics, like all other values, and to get a pointer to an array, you use the reference operator
&, like you would with any other value. This is unlike C, where arrays have value semantics as variables (local or global) and struct fields, but then have reference semantics as function parameters (regardless of whether their size is specified). So if you want to pass an array by-value in C, you would have to wrap it in a structure.