Arrays in C are data structures that hold multiple elements. C uses a numeric index to access these elements.
Things are best explained through examples, so here is one example.
Advertise on this site. I promise you will like the rates :)
This is an array, called "numbers" and it holds 10 values. The first row represents the index - the values 0 - 9. The second row of the picture are the actual values (23, 56 ... -40). We call these values elements. To access the elements we need to use the name of the array and the index.
For example, here the element with position 0 has a value of 23, index 4 points to the value -5 and so on.
Most of the time, you will use a loop to handle the values in an array. Usually, you will use a for loop and use its counter variable as an index.
Creating arrays in C is similar to how we create regular variables. Here we need to specify the type, the name and the size. To specify the size we use square brackets. Here is an example of how we can create a character array called "symbols" with size 5:
Then we can assign a value to, let's say, the first element:
And finally output that value to the screen:
In this case we are holding char values, but we can define the array to be of any other type. However, once we create it, we cannot change neither its type, nor its size.
Like we said above, usually you will use a loop to go through the elements. In this next example we use character arithmetic to save the letters from the English alphabet in an array called "letters":
Be alert that arrays in C don't have boundary checks. This means that you can access (read/write) values that are outside of the array. For example: in the above program we could have continued to write after index = 25. This means that we would overwrite whatever values were located in the memory after the array. It is obvious that this could cause a loss of data, program crash or other unintended consequences.
In the same way we could continue to read after the last element. Once again this may cause our program to behave in unexpected ways.
So far, we only looked at an array with one dimension. There we had one index and we visualized the elements as one row of vales.
A 2 dimensional array is usually represented like a table. It has two indices - one for the rows and another for the columns. It is accepted that the first is the row index. Just like before, the rules for the index are the same - they both start from 0 and reach the dimension size -1.
Here is one example - here we create an array, called 'prices', it has 4 rows and 5 columns.
We can visualize it like this:
To go through all elements in this case, we need to use nested loops. Because we know the exact number of rows and columns it is convenient to use for loops. Let's reset all prices to 0.0f:
You can create an array with more than 2 dimensions. In fact, there is not limit to how many dimensions arrays in C can have. However, it is very rare to use more than 2. In fact, if you ever need to use 3-4 or even more, you should reconsider your data representation. Most of the time you can organize your data more efficiently and you don't need a 3 dimensions. For 7 years of programming, I have used 3D array just once.
Just to make one example - here is how we create a 3 dimensional array and find the sum of all positive elements.
And remember that you can omit the brackets of the loops and ifs, when their body consists of only one row. Many people prefer this way, so you need to get used to reading both styles. The above code is equivalent to this:
And just make the example more interesting, here is the same data, but this time we are performing some arithmetic with the result in order to present it in a more convenient way: