1-D Arrays in C
An array is a fixed-size, contiguous sequence of memory locations that stores multiple items of the exact same data type under a single name.
Contiguous Memory
Stored back-to-back in consecutive RAM memory addresses.
0-Based Indexing
Indices run from 0 to N - 1 (offset from start).
Instant Access O(1)
Address = Base + (i × sizeof(type)) calculated in hardware.
Homogeneous Type
Every element must strictly share the identical data type.
Array Memory Layout & Address Calculation
In C, array name is a constant pointer to the base address. Explore how byte offsets are calculated based on data types!
Interactive Address Calculator
Address(arr[2]) = 2000 + (2 × 4) = 2008
Contiguous RAM Cells Visualization
i denotes the exact number of element blocks to skip from the base address! arr[0] skips 0 × sizeof bytes.
Array Traversal with for Loop
Step through visiting every element in the array sequentially to read or display its value.
Interactive Traversal Tracer
Finding Maximum & Minimum Element
Initialize max = arr[0] and min = arr[0], then compare every subsequent element i = 1 to N-1.
Interactive Min/Max Scanner
i = 0 | Val: 34 | MAX: 34 | MIN: 34
In-Place Array Reversal
Reverse an array in-place with O(N / 2) swaps by having two pointers converge from the ends to the center.
Two-Pointer Swap Simulator
left = 0 | Right: right = 5 | Status: Ready
Linear Search (O(N))
Sequential search checks each element one by one from index 0 until the target key is found or the array ends.
Interactive Search Scanner
arr[0] == 12 | Status: Ready
O(N²))Bubble Sort Visualizer
Repeatedly compares adjacent pairs, and swaps them if out of order. Largest element "bubbles" to end each pass!
Interactive Bubble Sorter
i = 0 | Pair: j = 0 vs j+1 = 1 | Ready
Common Array Pitfalls & Bugs
C provides no automatic array bounds checking for performance reasons. Understand how to avoid critical security and memory bugs.
1. Out-of-Bounds (Buffer Overflow)
Accessing arr[5] in int arr[5]; reads/writes adjacent memory causing random bugs or crashes!
2. Uninitialized Garbage Values
Local arrays are NOT initialized to zero automatically. They contain unpredictable memory junk.
3. Array Decay to Pointer
When passed to functions, arrays decay to pointers. sizeof(arr) inside a function returns pointer size (8B/4B)!
4. Off-By-One in Loop Limits
Using i <= N instead of i < N will cause the loop to run N + 1 times, accessing past the array boundary.