What Are Arrays?


An array is a block of memory locations that all hold the same type of information. There are lots of uses for arrays, but one of the most common in games is to create a group of enemies.

To get an idea of what an array is, imagine a group of mailboxes at a post office. Each individual mailbox has its own address, and can hold different information.

Note

This chapter will give you all the basics you need to understand and code arrays. For the practical application, hold tight: Part 5 is called "The Big Payoff" for a reasonwe'll use all the knowledge you've been building to actually create a game.


Arrays work very much like a group of mailboxes that you might see at an apartment building or post office. Each "mailbox" is called an array element. Every array element has its own address and holds one piece of data. Every array element is the same data type and the same size as every other array element.

For an example, take a look at Figure 10.1, which provides a graphical representation of an array of integers. All elements in this particular array must contain integers. None of the elements can be used for storing other types of data, such as floating-point numbers, because this array is specifically declared as an array of integers. However, each element in the array can hold a different integer value.

Figure 10.1. An array of 10 integer elements.


Arrays can hold any data type. For instance, you can declare arrays that hold floating-point numbers and others that hold sprites.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net