Skip to content
📖 Auto vs Modern CBook contents

Chapter 6: Derived Data Types ​

Level 1 — Acquaintance

Building complex data structures from basic types: arrays, pointers, structs, and type aliases.

C derives complex types from its basic building blocks. Arrays group elements of the same type contiguously in memory. Pointers reference other objects indirectly. Structs bundle heterogeneous fields into one unit. Type aliases give new names to existing types. Auto provides all of these with a cleaner syntax.


6.1 Arrays ​

An array is a fixed-size sequence of elements of the same type, stored contiguously in memory. The array size is part of its type and cannot change at runtime.

c
// C
int primes[5] = { 2, 3, 5, 7, 11 };
printf("first = %d\n", primes[0]);    // first = 2
printf("count = %zu\n", sizeof(primes)/sizeof(primes[0]));  // count = 5
auto
// Auto
let primes [5]int = [5]int{2, 3, 5, 7, 11}
print("first =", primes[0])
print("count =", len(primes))

Key differences:

FeatureCAuto
Declarationint a[5];let a [5]int
Size in typePrefix: int a[N]Suffix: [N]int
Array lengthsizeof(a)/sizeof(a[0])len(a)
Bounds checkingNone (undefined behavior)Runtime check (default)
Multidimensionalint m[3][4][3][4]int

Array-to-pointer decay: In C, an array name in most contexts decays to a pointer to its first element. This is the source of many subtle bugs. Auto does not have this decay — arrays remain arrays.

c
// C: decay in action
int a[5] = {1,2,3,4,5};
int *p = a;        // a decays to &a[0]
p[10] = 99;        // UNDEFINED BEHAVIOR: out of bounds

Takeaway: C arrays have no bounds checking. Accessing a[10] on a 5-element array compiles without error but corrupts memory. Auto inserts runtime bounds checks by default.

Released under the MIT License.