1D Arrays — Declaration, Initialisation & Access
An array in C is a contiguous block of memory holding elements of the same type. Array indices start at 0. The name of an array is not a variable — it is a constant referring to the address of the first element. There is **no bounds checking** in C — accessing `arr[n]` on an array of size `n` is undefined behaviour that typically corrupts adjacent memory or crashes.
1D Arrays — All Patterns & Memory Layout
#include <stdio.h>
int main(void) {
/* ── Declaration and initialisation ──────────────────── */
int scores[5]; /* uninitialized — contains garbage! */
int primes[5] = {2, 3, 5, 7, 11}; /* full init */
int zeros[10] = {0}; /* first elem=0, rest zero-filled */
int auto_size[] = {1, 4, 9, 16, 25}; /* compiler counts: 5 */
/* ── Safe length calculation ─────────────────────────── */
int n = (int)(sizeof(auto_size) / sizeof(auto_size[0]));
printf("auto_size has %d elements
", n); /* 5 */
/* ── Access and modify ───────────────────────────────── */
primes[0] = 2; /* first element, index 0 */
primes[4] = 11; /* last element, index n-1 */
/* ── Traversal ───────────────────────────────────────── */
printf("Primes: ");
for (int i = 0; i < 5; i++) printf("%d ", primes[i]);
printf("
");
/* ── Memory layout — elements are CONTIGUOUS ─────────── */
printf("Address of primes[0] = %p
", (void *)&primes[0]);
printf("Address of primes[1] = %p
", (void *)&primes[1]);
/* Each address is sizeof(int) = 4 bytes apart on most systems */
/* ── Aggregate sum ───────────────────────────────────── */
int sum = 0;
for (int i = 0; i < 5; i++) sum += primes[i];
printf("Sum of primes = %d
", sum);
/* ── Out-of-bounds access — UNDEFINED BEHAVIOUR ──────── */
/* primes[5] = 99; ← writes into memory after the array!
primes[-1] = 0; ← writes before the array!
C will NOT warn you. Both silently corrupt memory. */
/* ── Passing arrays to functions (array DECAYS to pointer) */
/* void print_arr(int *arr, int n) — prototype */
/* Discussed in Module 6 (Pointers) in depth */
return 0;
}Common Mistakes
- Off-by-one access — `arr[n]` is invalid for an array of size `n`. Valid indices are `0` to `n-1`. This is one of the most common C bugs and often causes silent memory corruption.
- Relying on uninitialised array values — `int arr[10];` contains garbage. Always initialise: `int arr[10] = {0};` or use `memset(arr, 0, sizeof(arr))`.
- Using a function parameter to get the array size — `sizeof(arr)` inside a function where `arr` is a parameter returns `sizeof(int*)` (pointer size), not the array size. Always pass the length as a separate parameter.
Tip
Tip
Practice 1D Arrays Declaration Initialisation Access in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Understanding memory layout is essential for efficient C programming
Practice Task
Note
Practice Task — (1) Write a working example of 1D Arrays Declaration Initialisation Access from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with 1D Arrays Declaration Initialisation Access is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- An array in C is a contiguous block of memory holding elements of the same type.
- Off-by-one access — `arr[n]` is invalid for an array of size `n`. Valid indices are `0` to `n-1`. This is one of the most common C bugs and often causes silent memory corruption.
- Relying on uninitialised array values — `int arr[10];` contains garbage. Always initialise: `int arr[10] = {0};` or use `memset(arr, 0, sizeof(arr))`.
- Using a function parameter to get the array size — `sizeof(arr)` inside a function where `arr` is a parameter returns `sizeof(int*)` (pointer size), not the array size. Always pass the length as a separate parameter.