Arrays in C++

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:05:00

What are Arrays in C++?

An array in C/C++, or any programming language, is a collection of similar data items stored at contiguous memory locations, with elements accessible randomly using array indices. They can be used to hold a collection of primitive data types such as int, float, double, char, and others of any type.

Array Properties in C++

  • An array is a collection of data of the same type that are stored in a contiguous memory area.
  • An array's index starts at zero. It means that the first element is stored at index 0 the second at index 1, and so on.
  • An array's elements can be retrieved using their respective indices.
  • Once an array is declared, its size is fixed throughout the program.
  • An array can have several dimensions.
  • Using the sizeof the operator, you may find out how many elements are in an array.
  • We can calculate the size of the type of elements stored in an array by subtracting adjacent addresses.

Initialization of Array in C++

In C++, array initialization involves assigning values to each of its elements. It is done either during or after the declaration. To initialize all elements, use a comma-separated list of values in curly braces {}, or assign specific values individually.

There are several ways to do this:

  • Use {} to initialize the variable at declaration.
  • Initialize an array without providing its size during the definition process.
  • Initialization using an element's index.

Accessing Array Elements in C++

To access an array element, use its index number. The array index begins with 0. The last element's index is n-1.


Types of Arrays

C++ has two types of arrays:

  1. Single-Dimensional Array
  2. Multi-Dimensional Array

 

Single-Dimensional Array

In C++, a single-dimensional array is a linear sequence of items of the same type that are accessible via a single index, such as int arr[10];, which specifies an array of 10 integers.

 

Multi-Dimensional Array

In C++, a multi-dimensional array is an array of arrays, with each element being an array that may be accessed using multiple indices. For example, int matrix[3][4]; defines a 2D array (or matrix) with 3 rows and 4 columns.

Two-dimensional arrays in C++

This type of array has two indexes to describe each element: the first index represents a row and the second index represents a column.

Three-dimensional arrays in C++

The 3D array operates in three dimensions. Each element in a 3D array is uniquely identified using three indices: the row index, the column index, and the depth index.

Array Traversal

Array traversal in C++ is the process of accessing each element of an array sequentially, generally using a loop (for, while, or do-while) to perform actions such as reading or altering the items.

Array Insertion

Insertion in an array in C++ refers to inserting a new element at a defined place, which frequently necessitates shifting existing items to the right to create room for the new one.

Array Deletion

In C++, deletion entails removing an element from a defined point and then shifting the other members to the left to fill the gap, preserving the array's continuous nature.

Size of an Array in C++

In C++, there is no length function to get array size like in Java, but we may compute the size of an array using the sizeof() operator method. First, we calculate the size of the entire array in memory and divide it by the size of the element type contained in the array. This will return the number of elements stored in the array.

Pointer to an array in C++

In C++, a pointer to an array is a variable that stores the memory address of the array's first element. This enables the indirect access and manipulation of array elements via pointer arithmetic.

Passing Arrays To Functions

In C++, arrays can be supplied to functions by stating their name without brackets in the function call. This passes the address of the array's first element, allowing the function to directly access and modify the elements.

Return an array from functions

In C++, returning an array from a function normally means returning a pointer to the array's first element. 

Advantages of Arrays in C++

  • Efficient memory use: Arrays allocate and access data in contiguous memory locations.
  • Easy access to elements: Elements are accessed using their index, which simplifies data retrieval.
  • Improved performance: Index-based access is faster than linked lists or trees and allows direct memory access.
  • Flexibility: Arrays can hold a variety of data kinds, including as integers, characters, and strings.
  • Easy algorithm implementation: Many algorithms make use of arrays, which makes it easier to implement them in C++.
  • Compatibility: Arrays can be used in complicated implementations with other data structures such as queues and stacks.
  • Easy to pass to functions: Arrays can be easily passed as function arguments, making data manipulation more efficient.

Disadvantages of Arrays in C++

  • Fixed-size: In C++, arrays are declared with a fixed size; there is no dynamic resizing.
  • Memory allocation: Arrays are allocated in contiguous memory blocks, which might present problems with huge arrays.
  • No bounds checking: C++ arrays do not include bounds checking, which can lead to out-of-bounds access and memory issues.
  • Limited flexibility: Fixed size and dimensions limit array flexibility, preventing dynamic resizing and manipulation.
  • Inefficient insertion & deletion: Adding or removing elements is inadequate since it requires shifting succeeding elements, which takes time in large arrays.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this