Skip to content
Educora
Intermediate18 min5 / 8

Arrays and strings

Store many values of the same type in an array, pass arrays to functions, learn that strings are character arrays ending in '\0', and use the string.h functions safely.

Check yourself
In this lesson you will learn
  • Declare and initialize an array and process its elements in a loop
  • Pass an array to a function together with its length
  • Explain the role of the '\0' character at the end of a string
  • Use strlen, strcpy, strcat and strcmp without causing a buffer overflow

A class has 25 students, and you need to store a mark for each of them. Creating 25 separate variables — mark1, mark2, …, mark25 — is tiring and awkward: you cannot walk through them with a loop. That is what an array is for: a row of values of the same type placed side by side in memory. In C, text is an array too — simply an array of characters.

Arrays

To declare an array, you write the type, the name and the number of elements in square brackets: int marks[5];. Elements are accessed by index, and indexes start at 0: the first element is marks[0] and the last is marks[4]. Starting values can be given in curly braces; if you give fewer values, the remaining elements become 0. For example, int counts[10] = {0}; sets every element to zero.

C
#include <stdio.h>

int main(void) {
    int marks[5] = {5, 4, 3, 5, 4};
    int n = sizeof(marks) / sizeof(marks[0]);
    int sum = 0;
    int best = marks[0];
    for (int i = 0; i < n; i++) {
        sum += marks[i];
        if (marks[i] > best) {
            best = marks[i];
        }
    }
    printf("Count: %d\n", n);
    printf("First: %d, last: %d\n", marks[0], marks[n - 1]);
    printf("Average: %.2f\n", (double) sum / n);
    printf("Best: %d\n", best);
    return 0;
}
Expected output
Count: 5
First: 5, last: 4
Average: 4.20
Best: 5

When you pass an array to a function, the function does not know its length, so the length is passed as a separate parameter. There is another difference: unlike an ordinary variable, the array is not copied — the function works with the original elements and can change them. You will see why in the next lesson, together with pointers. Writing const on the parameter of a function that does not change the array is a good habit.

C
#include <stdio.h>

int find(const int arr[], int n, int target) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == target) {
            return i;
        }
    }
    return -1;
}

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

int main(void) {
    int prices[4] = {3, 7, 1, 10};
    printf("7 is at index %d\n", find(prices, 4, 7));
    printf("5 is at index %d\n", find(prices, 4, 5));
    double_all(prices, 4);
    printf("After: %d %d %d %d\n", prices[0], prices[1], prices[2], prices[3]);
    return 0;
}
Expected output
7 is at index 1
5 is at index -1
After: 6 14 2 20
find returns −1, an index that cannot exist, when the value is not found. double_all changed the original array.
Interactive
Loading simulation…
This is exactly how find works: it checks the elements one by one, starting from index 0.

Strings: character arrays ending in '\0'

C has no separate string type. A string is a char array with a special null character '\0' at the end. Functions such as printf and strlen find the end of a string by looking for this character. When you write char word[] = "milk";, the compiler creates a 5-byte array: 4 letters plus '\0'.

Index01234
word[i]'m''i''l''k''\0'
This is how the string "milk" looks in memory: 4 letters and a null character at the end.
C
#include <stdio.h>
#include <string.h>

int main(void) {
    char word[] = "milk";
    printf("Word: %s\n", word);
    printf("strlen: %zu\n", strlen(word));
    printf("sizeof: %zu\n", sizeof(word));
    printf("First letter: %c\n", word[0]);
    word[0] = 's';
    printf("Changed: %s\n", word);
    return 0;
}
Expected output
Word: milk
strlen: 4
sizeof: 5
First letter: m
Changed: silk
strlen counts the characters before '\0', while sizeof counts every byte of the array.

The string.h functions

You cannot assign one array to another with = or compare strings with ==. For this you use the functions from the string.h header: strcpy(dest, src) copies a string, strcat(dest, src) appends one to the end, and strcmp(a, b) compares. strcmp returns 0 if the strings are the same, a negative number if a comes first in alphabetical order, and a positive number if it comes later.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char first[20] = "Baku";
    char second[20];
    strcpy(second, first);
    printf("Copy: %s\n", second);
    strcat(second, " city");
    printf("Joined: %s (%zu chars)\n", second, strlen(second));
    if (strcmp(first, "Baku") == 0) {
        printf("Equal\n");
    }
    if (strcmp("apple", "banana") < 0) {
        printf("apple comes first\n");
    }
    return 0;
}
Expected output
Copy: Baku
Joined: Baku city (9 chars)
Equal
apple comes first

Key points

  • An array stores values of the same type side by side; indexes go from 0 to n - 1.
  • C does not check array bounds — going past them is a serious bug.
  • Pass an array's length to a function along with the array; the function can change the original elements.
  • A string is a char array ending in '\0': "milk" takes 5 bytes, while strlen gives 4.
  • Copy strings with strcpy, compare them with strcmp, and always check the destination's size.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
What is the index of the last element of int a[10];?