ArraysArrays

In the previous lessons, we created variables to store some data, but what if we have a lot of data, for example, the grades of a hundred high school students? Agree creating a hundred different variables is somehow not environmentally friendly.

Arrays are used for such cases.

An array is a whole set of variables of the same type. If a variable is a kind of single box, then an array is a whole warehouse of such boxes.

And most importantly, each box is indexed. That is, we can refer to them.

An array declaration looks like this:

This is how the array declaration looks like, which will store 3 elements. Elements that will be stored in an array must be written in curly braces.

Array elements can be accessed using indexes.

Indexes

The index is the number of each element in the array, like your number in line at a coffee shop. With the help of indexes, we can access any element of the array. Index counting starts from zero, i.e., the number of the first element will be 0.

c

Main.c

Note

Such arrays are static, their size does not change during program execution. Dynamic arrays can also change their size while the program is running.

You can also declare an array in another way:

We don't have to specify the number of elements if we specify the elements explicitly.

At the compilation stage, the compiler sees how many elements are in the array and, without our help, can allocate the required amount of memory. This can be done if we work with an array of already known values.

You can't declare an array this way.

We can also change the values of array elements by accessing them by index:

c

main.c

Everything was clear?

Section 2. Chapter 4