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.
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.
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
Data_type Array_Name [size];
Depicted below is an example of a declaration of the array.
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
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.
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.
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
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.
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.
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.
In this type of array, two indexes describe each element, the first index represents a row, and the second index represents a column.
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.
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.
And below is the output of this example.
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:
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.
Now, let’s have a look at how to pass an array to a function.
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
void pass(int arr)
Here arr 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.
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:
By passing the whole array to the function:
void pass(int arr)
Here you are passing an unsized array arr, i.e., it passed the whole array into the function.
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:
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.