• $19 Game Dev' Courses - Use code "FEBUDEMY"

    Video courses

The Unreal Engine Developer Course – Learn C++ & Make Games

Learn and Understand C++

Over 260 video lectures in 44 hours of video!

Learn C++: Beginner to Advanced

Join thousands of successful students!

C++ arrays do exactly what their name implies. They allow us to handle whole arrays of data in one simple structure. Certainly, there is quite a bit to learn about arrays but actually they are very straight forward as we we will see.

What exactly, is an array?

If a variable is a box in which we can store a value of a specific type, like int, float or char, then we can think of an array as a whole row of boxes. The row of boxes can be of almost any size but can be of only one type.

Declaring an array

We can declare an array of 10  int type variables like this:

int aBunchOfInts [10];

Now we have an array called aBunchOfInts that can store 10 int values. At the moment, however, it is empty.

Accessing the elements of an array

To add values to the elements of an array we can use syntax we are already familiar with combined with some new syntax, known as array notation. In this next code, we store the value of 99 into the first element of the array:

aBunchOfInts [0] = 99;

  • Beginning C++ Game Programming

    Beginning C++ Game Programming

    This book is all about offering you a very fast and fun introduction to the world of game programming and C++. It will begin by teaching you the programming basics such as variables, loops, and conditions. In this book there is very little theorizing. If you want a deep discussion of all the ins and outs of C++ concepts look elsewhere. If you are starting from a knowledge of zero C++ and want to build great games- fast, take a look at this book.
    • Setup a C++/SFML/Visual Studio development environment
    • Build a fast, fun Timber clone after learning basic C++
    • Discover the fundamentals of C++ object oriented programming and build a multi-level, top-down 2D, scrolling zombie survival/shooter
    • Explore some advanced C++ concepts and make a side scrolling, co-op split-screen multiplayer platform game with directional sound, particle effects and your very own level designs!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)
    C++ for Game programmers book

    C++ For Game Programmers

    Already know the basics of C++ but want to dig much deeper in an entirely game-focussed context? C++ for Game Programmers is that book. This best selling title supplements your knowledge by putting everything into perspective from a game-development point of view. The book points out the most effective C++ practices and steers you away from the potentially dangerous ones. It describes common C++ techniques to solve specific problems most game developers face.
    • From a gaming perspective, learn about inheritance, performance, memory management, and STL to object creation, object serialization, and scripting languages
    • Explore C++ Design Patterns and high-level problem-solving constructs that are most commonly found in games
    • Discover custom Structures and algorithms, which provides a detailed hands-on discussion of using C++ to create efficient solutions to difficult problems in a highly structured manner
    Amazon USA
    Amazon UK
    Amazon CA
    Game Programming Patterns book

    Game Programming Patterns

    If you are serious about writing high quality, reusable, efficient code using C++ or indeed any other language, then inevitably you will need to seriously hone your knowledge of game programming patterns. If you don't know what game programming patterns are they are simply the solutions to design problems. If you have programmed even a basic game you will have noticed that the code gets long and hard to manage, very quickly. The more features you add the more sprawling and messy your code gets. In addition to usability/reuse-ability, our game code needs to be efficient enough for the game to run smoothly. That's what this book will do for you.
    • Learn to write a robust game loop, how to organize your entities using components, and take advantage of the CPUs cache to improve your performance
    • Discover how to organize your entities using components, and take advantage of the CPUs cache to improve your performance.
    • Never again wonder what is the "best" way to organize your game code
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)

To store a value of 999 at the second element we write this code.

aBunchOfInts [1] = 999;

We can store a value of 9 at the last element like this.

aBunchOfInts [9] = 9;

Note that the elements of an array always start at zero and go up to the size of the array take away one. Similarly to ordinary variables we can manipulate the values stored in an array. The only difference is that we would use the array notation to do so. In this next code, we add the first and second elements together and store the answer in the third.

aBunchOfInts [3] = aBunchOfInts [0] + aBunchOfInts [1];

Arrays can also interact seamlessly with regular variables, like this perhaps.

int a = 9999;

aBunchOfInts[4] = a;

Initializing the elements of an array

We can more quickly add values to the elements like this example that uses a float array.

float myFloatingPointArray [3] {3.14, 1.63, 99.0};

Now the values 3.14, 1.63 and 99.0 are stored in positions 1, 2 and 3 respectively. Remember that when using array notation to access these values we would use 0,1 and 2.

There are other ways to initialize the elements of an array. This slightly abstract example shows using a for loop to put the values 0 through 9 into the uselessArray array.

for(int i = 0; i < 10; i++){

     uselessArray[i] = i;

}

So what do these arrays really do for me?

The real benefit of arrays in our game code was hinted at the start of the tutorial. Arrays can hold objects (instances of classes).

Imagine that we have a ZombieEnemy class and we want to store a whole bunch of them. We could do so like this:

ZombieEnemy horde [5] {zombieEnemy1, zombieEnemy2, zombieEnemy3... etc};

The horde array now holds a load of instances of the ZombieEnemy class. Each one is a separate, living(kind of), breathing, self-determining Zombie Enemy. We could then loop through the horde array each pass through the game loop moving the zombies, checking if their heads have met with an ax or if they have managed to catch the player.

The best way to get a feel for this is to see it in action.