Static Arrays
Tags: c, beginners, array
Published: 2019-06-19 05:55:31

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:
```cpp
<array_type> <array_name> [<array_size>];
```
For example:
```cpp
int grades[100];
```
As you can see, the size of our array is limited, but it still much bigger and easier to use than:
```cpp
int grade1, grade2, grade3,...;
```
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:
```cpp
// GOOD
char word[40];

// BETTER
#define SIZE 40
char word[SIZE];

// ERROR! the value of 'n' isn't known before the compilation process!
int n = 40;
char word[n];
```
(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:
```cpp
int grades[100];
grades[0] = 100;
grades[1] = 95;
```
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:
```cpp
#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]);
    }
}
```
This code outputs:
```cpp
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
```
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!__

Regards,
Gilad