Your Guide to Arrays in C++

RedXIII | June 11, 2021, 4:17 p.m.

Arrays are used to store collections of data. This data structure is useful for grouping data of the same type together. In this guide to arrays in C++, you’ll learn how to create and use arrays in your own C++ programs.

We’ve included C++ coding samples to help you quickly understand the material. Coding is easier when concrete examples are available. It’s a good idea to try the following examples on your own computer, using either an IDE or text editor. 


If you’d like to learn more about getting started with C++, follow our guide on setting up a C++ compiler on Windows.

What is an Array?

Using arrays, variables of the same type can be represented by a single variable. For example, the integers ‘x’, ‘y’, and ‘z’ must be declared separately in C++. Using arrays, three integers can belong to the same variable. 


int x = 1;

int y = 2;

int z = 3;


// Declare an array with three elements

int nums[3] = {1,2,3};


cout << x << endl;

cout << nums[0] << endl;

Arrays make it easy to keep track of multiple variables at once, provided they are of the same type. Because arrays can hold a group of data, computer scientists refer to this type of data structure as a collection. It’s important to note that arrays also have a set length that is determined upon their creation.

How Do I Use Arrays in C++?

Arrays probably have an infinite number of uses. At least trying to count them all would be pointless. They can store numbers or string data, so anything that would require several of these types of data is a good candidate for an array.

Furthermore, arrays can hold structs, classes, and even other arrays. This greatly extends the capability of the array, and adds to the tremendous power and flexibility of this data structure.

Accessing the Elements in an Array

The data—or elements—in an array are accessed using an index. Because the elements in an array are in a specific order, they can be accessed by their place in the array. The index points to the location of the element in the array.

Don’t try to access a non-existing element of an array, by the way. You’ll run into trouble if your array index points to a position that’s greater than the length of the array. The same is true of any index that is less than 0. 


Referencing a number larger than the total length of the array can give strange results.


int nums[3] = {1,2,3};

// Uh oh! 100 is way outside the boundaries of the array

cout << nums[100] << endl;


Output

71303179

Creating an Array of Numbers in C++

In order to create an array in C++, you’ll need to tell the computer three things: the type of data the array holds, the name of the array, and how many elements are in the array. Arrays use square brackets to reference the elements they hold. Square brackets are also used to set the length of the C++ array. Here’s the syntax for creating an array:

type name[length] = {element1,element2};

Length refers to the total number of elements the array holds (in this case, length would be 2).


When creating a new array, the elements of the array are declared within curly brackets.The elements in an array need to be separated by a comma.

// create a new array of integers

  int digits[9] = {1,2,3,4};


To reference these numbers, we need to know their position in the array. To illustrate we’ll make an array of strings.


// create an array of strings

string colors[3] = {"red","blue","green"};

cout << colors[0] << endl;

Output

red

In the example above, we can see that the string “red” is in the first position of the array. The second position holds the string “blue.” In order to access these strings, it’s necessary to provide an index representing their position. 

To reference the first element in the string, we need to pass the index of the first position. In C++, the first position of an array is at index 0.

As you can see, arrays are ordered starting with the number 0. The second element in the array is accessed by the number 1, and so. This means that arrays are ordered from 0 to length-1.

Populating an Empty Array

When declaring a new array in C++, it isn’t necessary to include the elements. That is, it’s possible to create an array of empty elements. Having an empty array is convenient because there are times when we won’t know what the elements of an array will be ahead of time.


In the following example, we’ll create an empty array and assign its elements one-by-one using a for loop.

#include <iostream>

#include <string>


using namespace std;


int main(){


  int digits[10]{};


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

    digits[i] = i;

  }


  cout << digits[5] << endl;


  return 0;

}

Using a Constant to Declare the Array’s Length

It’s possible to use a constant to declare the length of an array in C++. In such cases, the constant can also be used to reference the length of the array in other contexts. For example, within a loop.

#include <iostream>

#include <string>


using namespace std;


int main(){


  const int array_length = 10;

  int digits[array_length]{};


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

    digits[i] = i;

  }


  cout << digits[5] << endl;


  return 0;

}

Creating an Array of Structs in C++

We’ve seen how to create an array of integers and an array of strings, but what about other types of arrays? In C++, we can create arrays out of other types of data structures too, including structs


The following example shows how to create an array of structs in C++. By referencing the position of a struct in the array, we can gain access to its member variables. This is a powerful feature that allows developers to create arrays using many data types.


#include <iostream>

#include <string>


using namespace std;


struct Bunny

{

  string name;

  int id;

};


int main(){


  Bunny bunnies[3];


  bunnies[0].name = "Zekarius";

  bunnies[0].id = 1;


  cout << "Arrays with Start Prism" << endl;

  cout << bunnies[0].name << endl;


  return 0;

}


Summary

Arrays are an excellent choice for storying collections of data. Because they group data of the same type, array’s make it easy to keep track of lots of data.


Understanding how to use arrays is an important step towards mastering computer programming. It’s a good idea to practice using arrays. Try creating some small test programs to try out your new C++ knowledge.

Related Posts

If you’d like to learn more about C++ and computer programming, follow these links to our other tutorials.


About Us

Learning at the speed of light.

We created Start Prism to help students learn programming. You can find exercises and recent tutorials below.

Topics Quizzes Tutorials

0 comments

Leave a comment