An Easy Guide To Understand The C++ Array

C++ is a popular programming language that has several concepts in it; one of them is Arrays. Arrays in C++ is an essential and widely used concept, and it is being used in almost all aspects of C++ programming. In this article, you will learn the C++ array in detail.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Is C++ Array?

An array can be defined as a group or collection of similar kinds of elements or data items that are stored together in contiguous memory spaces. All the memory locations are adjacent to each other, and the number of elements in an array is the size of the array. For example, imagine the parking lot of a mall that allows only parking for two-wheelers. Now this collection of similar types of wheelers in the parking lot can be called an array. 

Array_Example1

In this memory representation, 2, 7, 4, 8,1, 6 are the elements of the array stored in adjacent locations, and 0, 1, 2, 3, 4, 5 are the indexes of these elements. Index position starts from 0 and goes up to size-1. In this example, the index goes up to 5, but actually, there are a total of 6 elements.

Now you need to understand how to declare and initialize a C++ array.

Initialization and Declaration of an Array

To declare an array in C++, you need to define

  • The data type of elements in the array
  • The name of the array
  • The size of the array

Syntax:

              Data_type Array_Name [size];

Depicted below is an example of a declaration of the array.

Example:

                 int club[10];

Here int is the data type of the array elements, the club is the name of the array, and ten is the size of the array, which means ten blocks of memory space are created for the array club.  

Now to initialize an array in C++, there are two ways.

  • Initializing the array during the declaration
  • Assigning the value after the declaration

Automation Test Engineer Master's Program

To learn about the automation of web applicationsExplore Course
Automation Test Engineer Master's Program

Initializing the Array During the Declaration

You can initialize an array while declaring it, by assigning elements to the array during the declaration using curly braces around the elements separated by commas.

arrayinitialization

Fig: array initialization 

Assigning the Value After the Declaration

After the declaration of the array, you can assign elements to the array at each index.

Array_Example3

Fig: array Initialization

Now, you will have a look at the different types of C++ arrays.

Types of Arrays in C++

There are 3 types of an array in C++ :

  • One-dimensional array
  • Two-dimensional array
  • Multidimensional array

One-Dimensional Array:

In this type of array, it stores elements in a single dimension. And, In this array, a single specification is required to describe elements of the array.

1DimArray

Fig: One-dimensional array

The diagram above shows that it arranged all the elements row-wise in a single dimension, one after the other. 

Now, take an example of a one-dimensional array.

one-dimensionalarray

Fig: Example of a one-dimensional array

In this example, you are printing the elements 1,7,9,4,5 using for loop, and depicted below is the output of this example.

one-dimensionalarray_3

Two-Dimensional Array:

In this type of array, two indexes describe each element, the first index represents a row, and the second index represents a column.

Two-dimensionalarray  

                                Fig: Two-dimensional array 

As you can see, the elements are arranged row-wise and column-wise; in a two-dimensional array, there are i number of rows and j number of columns. The above figure is a representation of a 3 x 3 matrix, which means there are three rows and three columns in the array.

Now let’s look at an example of a two-dimensional array.

Two-dimensionalarray_2.

                                   Fig: Example of a two-dimensional array

In this example, you are printing a two-dimensional array of three rows and three columns; you need to use two for loops. The first loop, i.e., i loop, runs for the row from 0 to 3, and the second loop, i.e., j loop, runs for the column from 0 to 3.

Also Read: Tutorial to C++ For Loop with Syntax and Examples

And below is the output of this example.

wo-dimensionalarray_3PNG

Fig: Output

Multidimensional Array:

The simplest example of a multidimensional array is a 2-d array; a two-dimensional array also falls under the category of a multidimensional array. This array can have any number of dimensions.

The syntax for declaring a multidimensional array is:

Syntax:

                Datatype array_name [size 1][size 2] . . . . . [size n];

Here size1 size2 up to so on size n describes the number of dimensions; in the case of a 2-d array, there are only two dimensions, a multidimensional array can have any number of dimensions.

Example:  

                     int array[5][10][4];

Now, let’s have a look at how to pass an array to a function.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

How to Pass an Array to a Function

In C++, you can pass arrays to a function in the following ways.

By passing a particular element of the array to the function

Syntax:

               void pass(int arr[10])

              {

                body 

                }

Here arr[10] is an element that is at index 10 of the array, the pass is the name of the array, and void is the return type. 

Example: 

ArraytoFunction

In this example, you must pass the element of the array at index 3, to the function pass, and inside the function, then you should print that particular element with the message: The element is.

The output of the above example is:

ArraytoFunction_2

By passing the whole array to the function:

Syntax:

                void pass(int arr[])

              {

               body

               }

Here you are passing an unsized array arr[], i.e., it passed the whole array into the function.

Also Read: C++ Functions: Syntax, Types and Call Methods

Example:

ArraytoFunction_3                 

In this example, an array ‘arr’ is passed to the function pass, and the array is getting printed with the help of for loop inside the function.

The output of the above example is:

ArraytoFunction_4

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Conclusion

In this article on C++ arrays, you learned about initialization and declaration, different types of an array in C++, their syntax, examples, and then you saw how to pass an array to a function with the help of examples and other C++ concepts like C++ shell, Polymorphism and Friend Function in C++.

Are you looking to be an expert programmer? If yes, Simplilearn’s Post Graduate Program in Full Stack Web Development in collaboration with Caltech CTME should be your next stop. This world-class bootcamp delivered by global practitioners and featuring masterclasses from Caltech CTME faculty offers you work-ready training in over 30 in-demand full stack tools and skills with over lesson-end, phas-end and capstone projects to help you perfect the skills learners and become job-ready upon completion.

Do you have any questions regarding this article on C++ Array? If you do, then please put them in the comment section. We’ll help you solve your queries. Check out our next tutorial on C++ Vector.

Happy learning!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.