Arrays
Arrays allow us to store multiple values of the same data type in a group.
Let's say we're writing an application which needs to store 3 air temperature measurements throughout the day. It might make sense to create 3 scalar (single value) variables:
double sunriseTemp, noonTemp, sunsetTemp;
But what if we need to store measurements for every hour? Or every 15 minutes? There's got to be an easier way than declaring 24 or 96 different variables! Since they are considered composite (multiple values), arrays can come to the rescue!
Syntax
Declaration
Array syntax is similar to variable syntax, with one important difference: size, which tells us how many values will be in our group. The C syntax for an array declaration statement must include a data type, an identifier (or name), a size (between square brackets), and end with a semicolon.
For example, if we need to save 24 temperature measurements in our program, we can declare an array:
double temperatures[24];
Accessing Elements
There are many benefits to grouping values, but we will still need to access each value (called an element) one at a time. To do that, we'll use the square brackets again but instead of having the size inside, it will be the location (called the index). Index values can be any integer value between 0
and one less than the original size (inclusive).
WARNING! Index values always start at 0
. They do not start at 1
(and should almost never be used that way).
For example, we can access the first value in the temperatures array through the following expression:
temperatures[0]
Expressions in this form can be used anywhere you would use a "regular" variable.
temperatures[0] = 88.2
or
printf("%.2lf", temperatures[1]);
or
scanf("%.2lf", &temperatures[12]);
or
sum += temperatures[23];
or
temperatures[3] > 75
Accessing All Elements
Because array names only refer to the beginning memory address, they cannot be used on their own to refer to all of the values at once. If you want to do something like display all of the values in an array, each element must be accessed individually, using indexes (there is no shortcut for this).
Since the index of the first element starts at 0
, the last index value is one less than the original size, and each element is at the next index value, arrays and for loops are best friends! For example:
for(index = 0; index < 24; index++){
printf(".2lf ", temperatures[index]);
}
would display all of the values in the temperatures
array.
Macros
Macros are extremely useful for managing the size of arrays!
Now we only have to change 24
to 96
once at the beginning of the file if we measure every fifteen minutes instead of every hour.
Behavior
Before jumping into array behavior, it may help to take a refresher in Variable Behavior.
There's also no visible program behavior associated with array declaration. Again, the operating system is reserving memory, but instead of enough bytes for a single value, it reserves array size * number of bytes space.
It's pretty normal to start out thinking that an array name refers to the whole group of values, but it really only refers to the starting address of the array. Remembering that can help prevent bugs, especially when using arrays with functions.
To access individual elements, the compiler does some simple arithmetic with the index and the data type size to determine the correct location of that element.
This is why indexes always starts at 0
! The result of 0 * number of bytes + starting address is the starting address and first element!