Arrays
Learn how to define and access arrays, and why it's important to know how arrays are implemented in C .
We'll cover the following...
An array in C is a set of items that are stored in consecutive memory locations. So far we have seen how we can use a variable to store a single value. Arrays are useful as they allow us to deal with a collection of values using a single variable.
The physical location of each element in the array is specified by an integer called the index. In C, the first index always starts with 0 (unlike some other languages, like MATLAB, where indices start at 1).
Defining and indexing an array
As an example, let’s say we want to store a list of 5 grades. We can define an array as follows:
int grades[5];
This declaration says we want to set aside space in memory for 5 integer values, and we can refer to that block in memory using the variable name grades
. Note that we have only allocated the space in memory, we have not initialized any values of the array. Whatever values happened to be in ...