Arrays
Store many values of the same type side by side in memory, and loop over them safely.
- Declare, initialize and index arrays
- Loop over an array without going out of bounds
- Pass arrays to functions along with their length
An array is a fixed number of values of the same type, stored one after another in memory. Instead of score1, score2, score3... you write int scores[3] and reach each one by its index.
1#include <stdio.h>
2
3int main(void) {
4 int scores[5] = {90, 72, 85, 64, 99};
5
6 printf("First: %d\n", scores[0]);
7 printf("Last: %d\n", scores[4]);
8
9 scores[1] = 75; // change an element
10 printf("Second is now %d\n", scores[1]);
11 return 0;
12}First: 90 Last: 99 Second is now 75
Indexes start at zero
An array of 5 elements has indexes 0 to 4. The index is really an offset: "start of the array, plus this many elements". So scores[0] is the first element and scores[4] the last. There is no scores[5].
Looping over an array
The standard loop visits indexes 0 up to, but not including, the length. You can compute the length of an array (in the same function where it is declared) with sizeof:
1#include <stdio.h>
2
3int main(void) {
4 int scores[] = {90, 72, 85, 64, 99};
5 size_t count = sizeof scores / sizeof scores[0];
6
7 int total = 0;
8 for (size_t i = 0; i < count; i++) {
9 total += scores[i];
10 }
11 printf("%zu scores, average %.1f\n", count, (double)total / count);
12 return 0;
13}5 scores, average 82.0
With an initializer you can leave the size empty (int scores[] = {...}) and the compiler counts for you. If you give a size and fewer values, the rest are set to zero: int counts[26] = {0}; is the idiomatic way to zero an array.
Arrays and functions
When you pass an array to a function, C does not copy it. The function receives the address of the first element, which has an important consequence: inside the function, sizeof can no longer tell you the length. So you always pass the length as a separate parameter. It also means a function can modify the caller's array.
1#include <stdio.h>
2
3int find_max(const int values[], size_t length) {
4 int best = values[0];
5 for (size_t i = 1; i < length; i++) {
6 if (values[i] > best) {
7 best = values[i];
8 }
9 }
10 return best;
11}
12
13int main(void) {
14 int data[] = {3, 41, 7, 26, 9};
15 printf("Max: %d\n", find_max(data, 5));
16 return 0;
17}Max: 41
Two-dimensional arrays
int grid[3][4]; is 3 rows of 4 columns. Access elements with two indexes, grid[row][col], and loop with nested for loops. In memory it is still one flat run of 12 ints, row after row.
Key takeaways
An array of N elements has indexes 0 to N-1. C never checks bounds for you.
Loop with
for (size_t i = 0; i < length; i++). Get the length withsizeof array / sizeof array[0]where the array is declared.Arrays passed to functions are not copied: pass the length separately, and use
constif the function should not modify it.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write C
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.
Print in reverse
Read 5 integers into an array, then print them in reverse order separated by spaces, followed by a newline.
For input 1 2 3 4 5 print 5 4 3 2 1.
- 1 2 3 4 5
- Mixed numbers
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Above average
The first number in the input is n (at most 100), followed by n integers. Print how many of them are strictly greater than their average.
For 5 then 1 2 3 4 10 the average is 4, so the answer is 1.
- 1 2 3 4 10
- All equal
- Half above
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…