Code of the Day
BeginnerArrays and strings

Arrays: declaration, indexing, and iteration

Store multiple values of the same type in C arrays — declaration, indexing, iteration, and passing arrays to functions.

CBeginner11 min read
By the end of this lesson you will be able to:
  • Declare and initialise a fixed-size array in C
  • Access and modify elements by index
  • Iterate over an array with a for loop
  • Pass an array to a function and understand what is passed

So far every variable has held a single value. Arrays let you store a sequence of values of the same type in a contiguous block of memory. They are the simplest and most efficient in C, and every more complex structure (linked lists, trees, hash tables) is ultimately built from arrays and pointers.

Declaring an array

int scores[5]; /* an array of 5 ints */

This allocates space for five consecutive int values on the stack. The array elements are uninitialised — just like single variables, they contain whatever bytes happened to be in memory. Always initialise before reading.

Initialising at declaration

int scores[5] = {92, 78, 85, 91, 65};

The values in {...} initialise the elements in order. If you provide fewer initialisers than the array size, the remaining elements are zeroed:

int arr[5] = {1, 2}; /* {1, 2, 0, 0, 0} */
int zeroed[100] = {0}; /* all 100 elements are 0 */

You can also omit the size and let the compiler count:

int primes[] = {2, 3, 5, 7, 11}; /* compiler infers size = 5 */

Indexing

Arrays are zero-indexed: the first element is at index 0, the last at index n-1.

int scores[5] = {92, 78, 85, 91, 65};

printf("First: %d\n", scores[0]);  /* 92 */
printf("Last:  %d\n", scores[4]);  /* 65 */

scores[2] = 90; /* modify element at index 2 */

C does not check array bounds. Accessing scores[5] or scores[-1] is out of bounds — you are reading or writing memory you do not own. C will not stop you. The result is undefined behaviour: you might read garbage, corrupt other variables, or crash with a segmentation fault. Always ensure your indices are in range.

Iterating over an array

The canonical pattern:

#include <stdio.h>

int main(void) {
    int scores[] = {92, 78, 85, 91, 65};
    int n = 5;
    int total = 0;

    for (int i = 0; i < n; i++) {
        total += scores[i];
        printf("scores[%d] = %d\n", i, scores[i]);
    }

    printf("Average: %.1f\n", (double)total / n);
    return 0;
}

The variable n holds the array length. C arrays do not carry their length — you must track it yourself.

Getting the size with sizeof

You can compute the number of elements from the array's byte size:

int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
/* sizeof(arr) = 20 bytes; sizeof(arr[0]) = 4 bytes; n = 5 */

This works only when arr is declared in scope — once you pass the array to a function, the pointer you receive does not carry the size. Track the size separately.

Passing arrays to functions

When you pass an array to a function, C passes a pointer to the first element, not a copy of the whole array. The function can read and modify the original:

#include <stdio.h>

void fill_zeros(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        arr[i] = 0;
    }
}

void print_array(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main(void) {
    int data[] = {5, 3, 8, 1, 9};
    print_array(data, 5); /* 5 3 8 1 9 */
    fill_zeros(data, 5);
    print_array(data, 5); /* 0 0 0 0 0 */
    return 0;
}

The int arr[] parameter is identical to int *arr — both mean "pointer to int." The n parameter carries the length because the pointer does not.

A practical example: finding min and max

#include <stdio.h>

void find_min_max(int arr[], int n, int *min, int *max) {
    *min = arr[0];
    *max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] < *min) { *min = arr[i]; }
        if (arr[i] > *max) { *max = arr[i]; }
    }
}

int main(void) {
    int data[] = {34, 7, 23, 32, 5, 62};
    int n = 6;
    int min, max;
    find_min_max(data, n, &min, &max);
    printf("Min: %d, Max: %d\n", min, max); /* Min: 5, Max: 62 */
    return 0;
}

Where to go next

Next: multidimensional arrays — representing tables and matrices in C using nested arrays.

Finished reading? Mark it complete to track your progress.

On this page