Arrays

Home

Arrays

Arrays are a list of data of a given type, just like variables.  They have an array size (number of elements or things) and an index for each element.

The index describes the distance from the start of the array.  Thus the first index is always 0 (at the start), and the highest is the array size - 1:

C arrays

An array is a collection of data that holds a fixed number of values of the same type.

For example: if you want to store marks (grades for the Americans in the audience) of 100 students, you can create an array for it.

float marks[100];

(Stewart: Note that float is a decimal variable, unlike int).

(Stewart: Note also marks is British for grades.)

The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

  1. One-dimensional arrays
  2. Multidimensional arrays (useful but not required)

How to declare an array in C?

data_type array_name[array_size];

For example,

float mark[5];

Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold 5 floating-point (decimal) values.

Elements of an Array and How to access them?

You can access elements of an array by using an index (plural: indices).

Suppose you declared an array mark as above. The first element is mark[0], second element is mark[1] and so on.

C Array declaration

A few key notes:

  • Arrays have 0 as the first index, not 1. In this example, mark[0]
  • Index is the distance to the start of the array.
  • A mnemonic: Computers always start counting at 0
  • If the size of an array is n, to access the last element, the (n-1) index is used. In this example, mark[4]

How to initialize an array in C programming?

It's possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

Another method to initialize array during declaration:

int mark[] = {19, 10, 8, 17, 9};


Initialize an array in C programming

Here,

mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9

Important thing to remember when working with C arrays

Suppose you declared an array of 10 elements. Let's say,

int testArray[10];

You can use the array elements from testArray[0] to testArray[9].

If you try to access array elements outside of its bound, let's say testArray[10], there may not be a compile error. However, this will cause unexpected output (a logic error).

This tutorial was adapted by Mr. Stewart from this website.

Sizeof

A useful function that Arduino provides named sizeof  returns the size of an array in bytes

To get the number of elements in an array, you have to divide the size of the array by how many bytes is in one element.  For example:

int myIntArray[] = {0, 1, 2, 4, 8, 16}; // there are 6 integers, each integer is two bytes, thus there are 12 bytes in this array
int numberOfIntsInArray;
numberOfIntsInArray = sizeof(myIntArray) / sizeof(int); // sizeof(myIntArray) is 12, sizeof(int) is 2, so arraySize = 12 / 2 = 6


Built-in Array Example

1. TODO: Open the built-in example in the Arduino IDE (File->Examples->05.Control->Arrays).

2. TODO: Upload this code and notice the order that the LEDs blink.

3. TODO: Refer to this example whenever you need a refresher on array syntax.

Check Your Understanding (hint: use Array Example for reference)

1. An array has a size that describes the number of elements it contains.  To access this array at a specific element, use a(n) .  The lowest of these is always 0, and the highest is size - 1.

2. To declare an integer array called pinArray that goes 3, 14, 15, 9, the statement (line of code) is = {3, 14, 15, 9}; (the only space separates the array name from the array type).

3. In setup, the statement (line of code) to declare an integer variable numPins and assign it the number of elements in pinArray  using sizeof() is (put a space before and after each operator)

4. The for loop that will iterate a variable is   (for the initializationStatement, use the answer to question #1 from 0 to numPins. For the testExpression use numPins.)

(Just the first line, not the entire loop.  Use the spacing as in the built-in for Array ExampleDon't forget the opening curly brace.)



Previous Page