Arrays in C: 1D, 2D & Memory Arrangement
Store collections of homogeneous data in contiguous memory. Learn 1D and 2D arrays, matrix operations, and passing arrays to functions.
- An array is a fixed-size collection of items of the same data type stored contiguously in memory.
- Array indexing is 0-based: an array of size N has indices from
0toN-1. - The array name decays into a pointer to its first element in most expressions.
- C does NOT perform array bounds checking: accessing out-of-bounds indices causes undefined behaviour.
- 2D arrays are stored in memory in Row-Major order.
1D Array Declaration & Memory Layout
When you declare int arr[5];, C allocates 5 consecutive integer slots in memory. If an int is 4 bytes, the entire array occupies 20 continuous bytes.
If you access `numbers[10]` on an array of size 5, C will not throw an 'IndexOutOfBoundsException'. It will read whatever garbage bytes happen to be at that memory offset or cause a segmentation fault.
2D Arrays & Passing Arrays to Functions
A 2D array represents data in rows and columns, such as matrices or coordinate grids. In memory, C lays out elements row after row (Row-Major format).
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 1 correct(0 attempted)
If int arr[5] is stored starting at memory address 1000, and sizeof(int) is 4 bytes, what is the address of arr[3]?
Intermediate