EZ

Eduzan

Learning Hub

Eduzan
Eduzan / C++

Arrays

In C++, an array is a collection of elements of the same data type, stored in contiguous memory locations. It allows you to store multiple values in a single variable and access them using an index. The size of the array must be specified at the time of declaration, and it cannot be changed later

Declaring and Initializing Arrays

Here’s how to declare and initialize arrays in C++:

1. Single-dimensional Array: This is the simplest form of an array.

Example:

#include <iostream>
using namespace std;

int main() {
    // Declare and initialize a single-dimensional array
    int numbers[5] = {1, 2, 3, 4, 5};

    // Access and print array elements
    for (int i = 0; i < 5; i++) {
        cout << "Element at index " << i << ": " << numbers[i] << endl;
    }

    return 0;
}

Output:

Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5

2. Multi-dimensional Array: Arrays can have more than one dimension. A two-dimensional array is commonly used to represent matrices.

#include <iostream>
using namespace std;

int main() {
    // Declare and initialize a two-dimensional array
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

    // Access and print array elements
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "Element at [" << i << "][" << j << "]: " << matrix[i][j] << endl;
        }
    }

    return 0;
}

Output:

Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6

Important Points about Arrays in C++:

  • Indexing: Array indexing starts from 0. The first element is accessed using index 0, the second with index 1, and so on.
  • Size Limitation: The size of the array must be a constant expression and cannot be modified after declaration.
  • Memory Management: The size of the array affects memory usage. For large data sets, dynamic arrays (using new keyword) or std::vector from the STL (Standard Template Library) can be used for flexibility.

Dynamic Arrays

If you need to create an array whose size can be determined at runtime, you can use dynamic memory allocation.

Example:

#include <iostream>
using namespace std;

int main() {
    int size;
    cout << "Enter the size of the array: ";
    cin >> size;

    // Dynamically allocate an array
    int* dynamicArray = new int[size];

    // Initialize array elements
    for (int i = 0; i < size; i++) {
        dynamicArray[i] = i * 2;  // Assigning values
    }

    // Print the array elements
    for (int i = 0; i < size; i++) {
        cout << "Element at index " << i << ": " << dynamicArray[i] << endl;
    }

    // Free the allocated memory
    delete[] dynamicArray;

    return 0;
}

Output:

Enter the size of the array: 5
Element at index 0: 0
Element at index 1: 2
Element at index 2: 4
Element at index 3: 6
Element at index 4: 8

But we can have array of void pointers and function pointers. The below program works fine.

int main()
{
    void *arr[200];
}
End of lesson.