DEV Community

Gilad Ri
Gilad Ri

Posted on

Static Arrays

Let's say we have to write an application which can save student's grades and calculate his/her average. How can we do so?

We can initialize a variable for each test, and keep the grade as its value. But, how can we know how many tests our client will have? We are almost certainly not able to know this in advance We need a better solution.

The better solution (and probably the best one) is to use arrays. An array is a sequence of elements such that each element is like a variable. The array format is:

<array_type> <array_name> [<array_size>];
Enter fullscreen mode Exit fullscreen mode

For example:

int grades[100];
Enter fullscreen mode Exit fullscreen mode

As you can see, the size of our array is limited, but it still much bigger and easier to use than:

int grade1, grade2, grade3,...;
Enter fullscreen mode Exit fullscreen mode

and enough for our case (a student probably doesn't have more than 100 tests in one year...)

But, not only that the size of an array we declare in this way is limited, but also we cannot change its size later. Moreover, its constant size has to be known before the compilation! (What means it must be a macro or a raw integer). Therefore, this type of array is called: "Static Array". We will learn in the future how we can create an array with a dynamic size that we can choose during the run-time of our application. Some examples in order to summary what we just said:

char word[40];

#define SIZE 40
char word[SIZE];

// ERROR! the value of 'n' isn't known before the compilation process!
int n = 40;
char word[n];
Enter fullscreen mode Exit fullscreen mode

(Make sure you understand the last example before you continue to the next topic).

You can enter to the i'th element in an array using "[i]", for example:

int grades[100];
grades[0] = 100;
grades[1] = 95;
Enter fullscreen mode Exit fullscreen mode

The index of the first element of an array is always 0 (except in MatLab, for some reason). What means that the last element of array of size n is n-1. For example, in array of size 3 we have 3 elemnts with the indexes: [0][1][2].

One of the biggest advantages of using array (over variables) is that you can iterate over an array:

#include <stdio.h>
#define SIZE 3

void main() {
    // Initializes an array of grades
    int grades[SIZE];
    grades[0] = 100;
    grades[1] = 95;
    grades[2] = 98;
    // Iterates over the array
    for (int i; i < SIZE; i++) {
        printf("The grade of the %d'th test is %d\n", i+1, grades[i]);
Enter fullscreen mode Exit fullscreen mode

This code outputs:

The grade of the 1'th test is 100
The grade of the 2'th test is 95
The grade of the 3'th test is 98
Enter fullscreen mode Exit fullscreen mode

When we iterate over an array we have to make sure we don't try to access to an element with an index that is out of range of the array size. For example, if we have an array of size 10, the indexes that are bigger or equal to 10 are out of range. It is a little confusing sometimes, and therefore it's really important for you, as a programmer, to be alert and to make sure that any access to the array's element is to a valid index. The compiler cannot exceed this error in advance, which can cause a serious error during the run-time of your application. Be careful!

Always remember, it's much C-mpler than you thought!


Top comments (0)