C++ from Scratch to Advance / Array in C++ / Few Exceptions in Array
Home /C++ from Scratch to Advance /Array in C++ /Few Exceptions in Array

Few Exceptions in Array

When you are learning how to learn C++ step by step, you eventually reach a point where you expect arrays to behave just like the variables you’ve already mastered. You might think, “If I can add two integers with a + b, why can’t I add two arrays with arr1 + arr2?”
The truth is, arrays have a very specific set of “rules” or exceptions. Unlike normal variables, you cannot perform operations on an array “as a whole.” In this guide, we will look at exactly how operations like assignment, summation, and output differ when dealing with arrays.

Exceptions Between Normal Variable and Array

Here are the few exceptions or the differences between the basic operations on a simple variable and on an array.

1. The Assignment Exception: You Can’t Just Copy-Paste

With a normal variable, if you have int a = 5; and int b;, you can simply write b = a;. Now b is also 5.
The Array Difference: You cannot assign one array directly to another. If you have int arr1[size]and int arr2[size], writing arr1 = arr2; is the wrong way and will result in a compiler error.

2. The Summation Exception: No “Bulk” Math

For normal variables, sum = a + b; works perfectly.
The Array Difference: If you try to write total = arr1 + arr2;, C++ will not understand that you want to add the contents of the boxes together.

3. The Output Exception: Printing the “Row,” Not the “Label”

If you have int a = 10;, you can simply write cout << a; to see “10” on your screen.
The Array Difference: You cannot write cout << arr; to see all the numbers inside the array. Doing this is “wrong” and won’t give you the values you expect.

Relatable Daily Life Example: The Tray of Water Glasses

Imagine a Normal Variable is a single glass of water. If you want to move the water, you just pick up the glass and move it. Simple!
Now, imagine an Array is a tray containing 10 glasses of water.
  • Assignment/Sum: If you want to “pour” the water from one tray into another, you can’t just tilt the whole tray and expect it to work perfectly. You have to pick up each glass one-by-one and pour it into the corresponding glass on the other tray.
In C++, the Loop is your hand that picks up each glass one-by-one.

The Reason

C++ forbids assigning built-in arrays because the array name decays into a constant pointer to a fixed memory location, which cannot be reassigned. Additionally, raw arrays lack object awareness, so the = operator does not know how many bytes to copy.

The Solution

1. Using a Loop

    • This is the most basic approach. It manually copies each value across the contiguous memory slots one by one using the subscript operator ([ ]).
#include <iostream>

int main() {
    const int SIZE = 5;
    int source[SIZE] = {10, 20, 30, 40, 50};
    int destination[SIZE];

    // Using a manual loop to copy element by element
    for (int i = 0; i < SIZE; ++i) {
        destination[i] = source[i];
    }

    // Print destination to verify the copy
    std::cout << "Loop Copy Result: ";
    for (int i = 0; i < SIZE; ++i) {
        std::cout << destination[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

2. Operator Overloading

  •  This approach wraps a raw array inside a custom struct or class. It lets you redefine how the = operator behaves, running a loop under the hood so the user can cleanly type array2 = array1.
  • Note: Operator overloading is an advance concept in OOPs. It will be explained later.

3. std::array

 This is the official modern C++ solution. The language creators used the exact same operator overloading trick mentioned above to build a safe, ready-to-use standard container.

 

#include <iostream>
#include <array> //Header file for array operations

int main() {
    // std::array allows direct assignment out of the box
    std::array<int, 5> source = {10, 20, 30, 40, 50};
    std::array<int, 5> destination;

    // Direct assignment works because std::array overloads the = operator  
    destination = source;

    // Print destination to verify the copy
    std::cout << "std::array Copy Result: ";
    for (int val : destination) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}


Interactive Elements: Try It Yourself!

  1. Reflection Prompt: Why do you think C++ forces us to handle arrays “one-by-one” instead of allowing arr1 = arr2? (Hint: Think about how the computer knows how many boxes are in the tray).
  2. Challenge 1: Write a small program that takes an array of 3 numbers {1, 2, 3} and uses a loop to add them. Your sum should start at 0. What is the final result?.
  3. Challenge 2: Try to print an array in reverse order. If your index i starts at size - 1, what should your loop condition and update (++ or --) be?.
  4. Logic Test: If you have an array with values {1, 2, 3} and you use a loop to assign new values {3, 4, 5, 6, 7} from a user, what happens to the original {1, 2, 3}?

Ready to Master the Logic?

For official technical deep-dives on how arrays are handled, check out the official C++ Documentation.
Happy Coding!
Video Tutorial