Module 5 · Arrays and Strings — topic 1 of 3
Arrays
An array stores a fixed number of elements of one type in contiguous memory. Learn declaration, initialisation, indexing, traversal, passing arrays to functions and why C never checks bounds.
Learning objectives
- Declare, initialise and traverse one-dimensional arrays
- Compute an array's length with sizeof
- Pass arrays to functions and explain array-to-pointer decay
Definition
An array is an object consisting of a contiguous sequence of elements of the same type, accessed by an integer index starting at 0. Its size is fixed when it is created.
Introduction
Storing 60 students' marks in 60 separate variables is impossible to manage. An array gives them one name and a number: marks[0] to marks[59]. Loops can then process all of them with a few lines.
Arrays in C are close to the hardware: elements sit side by side in memory, and a[i] is computed from the starting address plus i times the element size. That makes arrays fast — and makes running off the end dangerous, because C does not check.
Why it matters
Arrays are the foundation of almost every data structure — stacks, queues, heaps, hash tables and matrices are built on them.
Questions on traversal, searching, finding max/min, reversing and counting frequencies appear in every C exam.
Detailed explanation
Declare with a constant size: int marks[5];. Initialise with a brace list: int marks[5] = {70, 82, 65, 90, 58};. Missing initialisers are set to zero, so int counts[26] = {0}; zeroes all 26.
If you omit the size, the compiler counts the initialisers: int primes[] = {2, 3, 5, 7}; has 4 elements. Designated initialisers (C99) set specific positions: int a[10] = {[3] = 7, [9] = 1};.
The number of elements is sizeof arr / sizeof arr[0] — but only where arr is the real array, not a pointer parameter.
When an array is passed to a function it decays to a pointer to its first element. The function receives an address, not a copy, so it can modify the caller's elements, and it cannot find the length itself — pass the length as a separate parameter.
Accessing a[5] in a 5-element array is undefined behaviour. It may read garbage, corrupt another variable or crash. C does not throw an exception.
Syntax
- Valid indexes are 0 to SIZE − 1.
- Arrays cannot be assigned as a whole (
a = b;is an error); copy element by element or withmemcpy. const int arr[]in a parameter promises the function won't modify the elements.
Code examples
Traverse, sum and find the maximum
n=5 sum=365 max=90 avg=73.0
Zero-initialisation and designated initialisers
1 2 0 0 0 0 0 0 9 0 0 4
More examples
Functions that work on arrays
4, 8, 15, 16, 23, 42 42, 23, 16, 15, 8, 4 1 appears 2 time(s) 2 appears 1 time(s) 3 appears 2 time(s) 4 appears 1 time(s) 5 appears 3 time(s) 6 appears 1 time(s) 9 appears 1 time(s)
Step-by-step explanation
reverseswaps from both ends toward the middle using two indexes in onefor(comma operator).- Because the array decays to a pointer,
reversechangesdatainmain— no copy is made. printtakesconst int arr[], documenting and enforcing that it only reads.freq[digits[i]]++uses each digit as an index into a counting array — a frequency table.- Only digits with a non-zero count are printed.
Expected output
Running “Functions that work on arrays” prints exactly:
Practical applications
- Audio is an array of samples; images are arrays of pixels.
- Network buffers are arrays of bytes.
- Histograms and frequency counts use the counting-array technique.
Best practices
- Keep the length next to the array — as a constant or a parameter.
- Loop with
i < n, neveri <= n. - Use
constfor array parameters a function only reads.
Common mistakes
Using sizeof on an array parameter
Avoid
Prefer
Inside the function arr is a pointer, so sizeof arr is the size of a pointer (typically 8), not of the array.
Reading past the end
Avoid
Prefer
The last valid index of a 3-element array is 2. a[3] is undefined behaviour.
Assigning arrays
Avoid
Prefer
Arrays are not assignable in C. Copy the elements with a loop or memcpy(b, a, sizeof a).
Important notes
- C99 introduced variable-length arrays (
int a[n];with runtimen); C11 made them optional. Large VLAs can overflow the stack; prefermallocfor big or user-sized arrays. - Tools such as AddressSanitizer (
-fsanitize=addressin GCC/Clang) detect out-of-bounds accesses while testing.
Key takeaways
- Arrays hold a fixed number of same-type elements, indexed from 0.
- Length =
sizeof arr / sizeof arr[0], valid only on the real array. - Arrays decay to pointers when passed; pass the length separately. Bounds are never checked.
Try it yourself
- 1Read 10 integers and print the second-largest distinct value.
- 2Write a function that rotates an array left by k positions.
- 3Remove duplicates from a sorted array in place and return the new length.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
10 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is the index of the last element of `int a[10];`?
- BeginnerWhat is printed (int is 4 bytes)?
- BeginnerAccessing `a[5]` in `int a[5];` causes…