Lesson 15 of 18

Arrays and Pointers

Arrays and Pointers

HolyC arrays and pointers work exactly like C. An array is a contiguous block of memory, and the array name decays to a pointer to its first element.

Declaring Arrays

I64 numbers[5];          // 5 I64 values, uninitialized
I64 primes[4] = {2, 3, 5, 7};  // initialized

Accessing Elements

Use zero-based indexing with []:

I64 arr[3] = {10, 20, 30};
Print("%d\n", arr[0]);  // 10
Print("%d\n", arr[1]);  // 20
arr[2] = 99;
Print("%d\n", arr[2]);  // 99

Pointers

A pointer holds the memory address of a value. The & operator gets an address; the * operator dereferences it:

I64 x = 42;
I64 *ptr = &x;   // ptr points to x
Print("%d\n", *ptr);   // 42 — dereference to get the value
*ptr = 100;
Print("%d\n", x);      // 100 — x was modified through ptr

Pointer Arithmetic

Moving a pointer by an integer moves it by that many elements:

I64 arr[3] = {10, 20, 30};
I64 *p = arr;       // p points to arr[0]
Print("%d\n", *p);       // 10
p++;                // advance to arr[1]
Print("%d\n", *p);       // 20

Strings as U8 Pointers

Strings are U8 * — a pointer to an array of bytes terminated by 0:

U8 *msg = "HolyC";
Print("%s\n", msg);  // HolyC

Your Task

Declare an array I64 vals[5] and fill it with the squares of indices 0–4 (i.e., 0, 1, 4, 9, 16). Then print each value on its own line.

Expected output:

0
1
4
9
16
Aiwnios HolyC loading...
Loading...
Click "Run" to execute your code.