C++ Array [With Examples]

Introduction

C++ Array, or Arrays in any programming language, is a collection of similar data objects stored in contiguous memory regions, and components in an array may be retrieved randomly using array indices. They may be used to hold a collection of primitive data types of any kind, such as int, float, double, char, and so on. In addition, a C++ array may hold derived data types such as structures, pointers, and so on. 

Why do we need arrays?

When we have a small number of objects, we can use regular variables (v1, v2, v3, etc.), but when we have a huge number of instances, it becomes impossible to handle them using normal variables. An array’s purpose is to store many instances in a single variable.

Array declaration in C++

An array can be declared in a variety of ways. It is possible to do so by declaring its type and size, by initializing it or by doing both.

Code 01: Array declaration by specifying the size

#include <iostream>
int main() {
    
// Array declaration by specifying size
int array1[10];
int a = 10;
int array2[a];

}

Code02: An Array declaration by initializing elements

#include <iostream>
int main() {
    
// Array declaration by initializing elements
int array[] = { 1, 2, 3, 4 }

}

Code 03: Array declaration by specifying the size and initializing elements

#include <iostream>
int main() {
    
// Array declaration by specifying size and initializing
int arr[5] = { 1, 2, 3, 4 }

}

Advantages and Disadvantages of C++ Array

Advantages

  1. Random access of elements using the array index.
  2. Use of fewer lines of code as it creates a single array of multiple elements.
  3. Easy access to all the elements.
  4. Traversal through the array becomes easy using a single loop.
  5. Sorting becomes easy as it can be accomplished by writing fewer lines of code.

Disadvantages

  1. Allows for the entry of a set number of items, which is determine at the time of declaration. An array in C++, unlike a linked list, is not dynamic.
  2. Element insertion and deletion can be costly since the elements must be handled in line with the new memory allocation.

Code Examples

Accessing C++ Array Elements

An integer index is use to access array items. The array index begins with 0 and continues until the array size is less than one.

#include <iostream>
using namespace std;

int main()
{
	int arr[4];
	arr[0] = 2;
	arr[2] = -5;

	arr[3/2] = 1;
	arr[3] = arr[0];

	cout << arr[0] << " " << arr[1] << " " << arr[2] << " "
		<< arr[3];

	return 0;
}

Output:

2 1 -5 2

C++ Array: No Index Out of Bound Checking

Because there is no index out of bounds checking in C++, the following program compiles Alright but may produce unexpected results when run.

#include <iostream>
using namespace std;

int main()
{
	int arr[5];

	cout << arr[2] << " ";
	cout << arr[-5] << " ";

	return 0;
}

Output:

-654606112 32766

Note: In C++, the program will not compile. When we save the preceding program as .cpp, it produces the compiler message error: too many initializers for ‘int [5]'”.

The elements are store at contiguous memory locations

#include <iostream>
using namespace std;

int main()
{

	int array[5], i;

	cout << "Size of integer in this compiler is "
		<< sizeof(int) << "\n";

	for (i = 0; i < 5; i++)
	
		cout << "Address array[" << i << "] is " << &array[i]
			<< "\n";

	return 0;
}

Output:

Size of integer in this compiler is 4
Address array[0] is 0x7ffd5ee16db0
Address array[1] is 0x7ffd5ee16db4
Address array[2] is 0x7ffd5ee16db8
Address array[3] is 0x7ffd5ee16dbc
Address array[4] is 0x7ffd5ee16dc0

Traversing a C++ Array

#include <iostream>

using namespace std;

int main()
{
	int array[5]={1,2,3,4,5};
	// Way 1
	for(int i=0;i<5;i++)
		cout<<array[i]<<" ";
	
cout<<endl;
	return 0;
}

Output:

1 2 3 4 5 

FAQs

Array vs Pointers

Arrays and pointers are not the same things (we can check by applying sizeof). The misunderstanding arises because the array name represents the location of the first element, whereas arrays are always pass as pointers (even if we use a square bracket).

What is a vector in C++?

In C++, a vector is an STL class that represents an array. The advantages of vector arrays over regular arrays are as follows:

  • When declaring a vector, we do not need to give size as an additional argument, indicating that vectors accept dynamic sizes (we do not have to initially specify the size of a vector). A vector can also be resize.
  • Vectors offer several built-in functions, such as eliminating an element.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. Float vs Double

Leave a Comment