By Stephen R. Davis

You may think of a variable in C++ as a truck. There are small trucks, like a short int, capable of holding only a small value; and there are larger trucks, like a long double, capable of holding astoundingly large numbers. However, each of these trucks can hold only a single value.

Each truck has a unique designator. Perhaps you give your vehicles names, but even if you don’t, each has a license plate that uniquely describes each of your vehicles, at least within a given state.

This works fine for a single family. Even the largest families don’t have so many cars that this arrangement gets confusing. But think about an auto rental agency. What if they referred to their cars solely by a license plate number or some other ID? (Boy, just thinking about that Hertz!)

After filling out the myriad forms — including deciding whether you want the full insurance coverage and whether you’re too lazy to fill it up with gas before you bring it back — the guy behind the counter says, “Your car is QZX123.” Upon leaving the building and walking to the parking lot, you look over a sea of cars that rival a Wal-Mart parking lot. Exactly where is QZX123?

That’s why the guy behind the counter actually says something quite different. He says something like, “Your car is in slot B11.” This means that you are to skip past row A directly to row B and then start scanning down the line for the eleventh car from the end. The numbers are generally painted on the pavement to help you out, but even if they weren’t visible, you could probably figure out which car he meant.

Several things have to be true in order for this system to work:

  • The slots have to be numbered in order (B2 follows B1 and comes immediately before B3), ideally with no breaks or jumps in the sequence.

  • Each slot is designed to hold a car (a given parking slot may be empty, but the point is that you would never find a house in a parking slot).

  • The slots are equally spaced (being equally spaced means that you can jump ahead and guess about where B50 is without walking along from B1 through B49, checking each one).

That’s pretty much the way arrays work. You can give a series of numbers a single name. Refer to individual numbers within the series by index. So the variable x may refer to a whole series of whole numbers, x(1) would be the first number in the series, x(2) the second, and so on in sequence, just like the cars at the rental agency.