Basic Arrays

Overview

Declaring and Allocating Arrays

Very soon, you will need to separate in your mind declaration from allocation.
Declaring a variable means that you specify the name and the type.
Allocating a variable is when the space where that variable will reside is created.
For the variables we've seen so far, those two things happen at the same time. This pairing of the two is about to end.

Initializing Arrays

Initializing a variable is placing the first values into that variable. Often, the safest thing to do is to provide a starting value at the same time as declaration (and allocation). However, this will soon not be able to occur in a single line of code. Below are ways to initialize arrays at the point of declaration and allocation.

Designated Initializers:

int numbers[ 100 ] = { 1, 2, 3, [10] = 10, 11, 12, [60] = 50, [42] = 420 };

Using Arrays

You may not operate on an entire array at once. The only way to operate "on an array" is to choose individual elements on which to perform calculations.

Sample Programs Using 1-D Arrays