Static Arrays Analysis

Array

An array is a data structure that stores elements of the same data type in a contagious memory location. These elements can be accessed by their index.

That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be accessed using the same identifier, with the proper index. 

Static Array

A static array is an array that has a fixed size. This means that once we define the array's size , we cannot change it during the program's execution.

Declaring an Array

In C++, you can declare an array by specifying its data type, followed by the array name and the size of the array in square brackets. For example:

int myArray[5]; // Declares an integer array of size 5

Syntax:

dataType arrayName[arraySize];
  • dataType: The data type of the array elements. It can be int, string, or char etc.
  • arrayName: The variable name we give to the array.
  • arraySize: The count of total elements in the array.

Access Elements in C++ Array

In C++, each element in an array is associated with a number. The number is known as an array index. We can access elements of an array using those indices.

// syntax to access array elements
myArray[index];

C++ Array Initialization

In C++, its possible to initialize an array during declaration. There are different ways to initialize arrays which are listed below.

Initializer List:

int myArray[] = {1, 2, 3, 4, 5}; // Initialize with values, size is automatically determined

Explicit Initialization:

int myArray[5] = {1, 2, 3, 4, 5}; // Explicitly specify size and initialize

Partial Initialization:

int myArray[5] = {1, 2}; // Initializes first two elements, rest are set to 0

Accessing Array Elements:

Array elements are accessed using their index, starting from 0. For example:

int element = myArray[2]; // Accesses the third element (index 2)

Array Size:

You can get the size of an array using the sizeof() operator.

int size = sizeof(myArray) / sizeof(myArray[0]); // Gets the size of the array

Array Bounds:

Be cautious not to access elements outside the bounds of the array, as it can lead to undefined behavior and memory corruption.

Iterating Over Arrays:

You can use loops to iterate over array elements. Here's an example using a for loop:

for (int i = 0; i < size; i++) {
    cout << myArray[i] << " ";
}

In modern C++ (C++11 and later), you have additional features like range-based for loops for easier iteration over arrays:

for (int element : myArray) {
    cout << element << " ";
}

Accessing Elements Through Pointers:

We can access array elements using pointers. Arrays are essentially contiguous blocks of memory, and you can use pointers to navigate through this memory and access individual elements.

#include <iostream>

int main() {
    int arr[] = {10, 20, 30, 40, 50};

    // Define a pointer to int
    int *ptr;

    // Point the pointer to the first element of the array
    ptr = arr;

    // Access elements using pointer notation
    // You can use pointer arithmetic to move through the array
    // For example, to access the third element (30):
    int thirdElement = *(ptr + 2); // The "+ 2" moves the pointer two positions ahead

    // Print the value
    std::cout << "Third element: " << thirdElement << std::endl;

    return 0;
}

// output
Third element: 30

Advantages of an Array in C++

  • Elements of an array can be accessed in O(1) time.
  • Minimizes the length of the code by putting values of multiple variables into a single array.
  • Updation of array elements is done in constant time.
  • Arrays can be easily traversed using a single loop.
  • Managing and sorting array elements requires fewer lines of code.

Disadvantages of Static Array in C++

  • The size of an array can not be increased or decreased once defined during declaration. The use of array is not suitable when size is not defined earlier.
  • Arrays are homogeneous. All the elements of an array should belong to the same datatype.
  • The process of insertion and deletion of elements in arrays is costly. This is because of enhanced complications in traversing to the point of insertion and deletion and shifting of array elements. New memory allocation creates the process of insertion more complex.
  • Garbage value is thrown while accessing any index out of range. Arrays in C++ do not throw index-bound errors during compile time.