C++ from Scratch to Advance / Array in C++ / Accessing and Updating Array Elements
Home /C++ from Scratch to Advance /Array in C++ /Accessing and Updating Array Elements

Accessing and Updating Array Elements

In previous lessons you’ve already discovered that an array is like a container for multiple items. But how do you actually reach inside that container to grab or change a specific item?
In this lesson, we move beyond just declaring arrays to handling them—learning the secrets of indexing, why memory addresses matter, and how the computer keeps everything organized in a straight line.

Definitions

Term Definition
Subscript Operator ([]) The square brackets are used to pinpoint a specific “element” inside your array.
Index The numerical address or “seat number” of an element, which always starts at zero.
Accessing The act of either looking at a value (reading) or putting a new value into a specific spot (writing).
Contiguous Memory A storage method where data is placed in one long, unbroken chain, one after another.
Base Address The memory location of the very first element in an array.
Byte-Addressable The way a computer gives a unique address to every single byte of memory.

How to Access Array Elements

To handle an array element, you need two things: the name of the array and the index of the item you want.
The Logic of Indexing
In C++, we don’t start counting at 1; we start at 0.
  • First Element: Located at arrayName[0].
  • Second Element: Located at arrayName[1].
  • Individual Variables: Once you access an element (like marks[1]), it behaves exactly like a normal, single variable. You can print it using cout or assign it a new value using =.

Example:

#include <iostream>
int main() {
    // Declaration and Initialization of a fixed-size array
    int scores[5] = {85, 92, 78, 90, 88};

    // Accessing and modifying an element using the subscript operator
    scores[2] = 95; // Changes 78 to 95

    // Printing array elements using a standard for loop
    std::cout << "Array elements: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << scores[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

How to Update Array Elements

In C++, updating an array’s element is as similar as updating a simple variable, all you have to do is adding the index after the varriable name.

Syntax:

array-name[index]= value;
//example
int arr[5] = {5, 4, 3, 2, 1}; // values stored in the arr is: 5, 4, 3, 2, 1
arr[0] = 10; // values stored in the arr is: 10, 4, 3, 2, 1

Assigning an array to another array;

int arr1[5] = {5, 4, 3, 2, 1};
int arr2[5] = {1, 2, 3, 4, 5};
for(int i=0; i<5; ++i){
   arr2 = arr1[i]; 
}
//values of the arrays:
//arr1: 5, 4, 3, 2, 1
//arr2: 5, 4, 3, 2, 1

Finding the Last Element: The “Size – 1” Rule

If you have an array with 5 slots, your indices are 0, 1, 2, 3, and 4. Notice that the last index (4) is actually the Size (5) minus 1.
  • Formula: Last_Index = Size - 1.
  • Example: If your ‘array size’ is stored in a constant variable called SIZE, you can always find the last item by typing arrayName[SIZE - 1].

Relatable Daily Life Example: The Parcel Delivery

Imagine you have three friends living in the same neighborhood, and all three are named “Ali”.
  • If you send a parcel just addressed to “Ali,” the delivery driver will get confused—which Ali do you mean?.
  • To get the parcel to the right person, you need a specific Address or Location.
In an array, every “box” has the same name (the array name), but the Index is the specific house number that ensures your data goes to the exact right spot in memory.

Understanding Contiguous Memory

Arrays don’t just store data anywhere; they store it in contiguous memory locations, meaning one right after another.

The “Four-Byte Jump”

Most integers in C++ take up 4 bytes of memory.

If the first element (Base Address) is at position 100, the next one isn’t at 101.

Because the first integer takes up 4 bytes, the next element will start at 104, the one after that at 108, and so on.

The Advantage: Because they are in a perfect sequence, if the computer knows the address of the first item, it can instantly calculate where any other item is located just by knowing the size of the array.

Why Addresses Matter

In the “old” way of storing variables, data might be scattered all over your memory. If you wanted to change a “5” to a “6,” you might accidentally change the wrong “5” if they didn’t have clear addresses.
With arrays, the Address acts as a GPS coordinate. Whether you are deleting an element or inserting a new one, the address ensures you are working on the exact piece of memory you intended.

Interactive Elements: Try It Yourself!

  1. Challenge 1: If you have an array of 10 integers and the first address is 500, what would be the address of the third element (index 2)? Hint: Size of a single integers element is 4 bytes
  2. Challenge 2: Write a single line of code to change the value at the zero index of an array named prices to 99.
  3. Logic Test: If you want to print the very last element of an array with 100 items, what index number will you put inside the []?
  4. Pattern Search: In your daily life, find one example of something stored “contiguously” (like lockers or a row of houses) and explain how you would “index” them.

Engagement Boost: Let’s Chat!

  • What’s your biggest C++ fear? Is it the math of memory addresses or just remembering to start at zero?
  • Question for the comments: If you knew the “Base Address” of an array, do you think you could find the 100th element without looking at the 99 items in between? Share your theory!

Take the Next Step

Understanding how to access and update elements is the key to building dynamic programs. For more technical details on how C++ handles memory, you can visit the official C++ documentation on Arrays.
Happy Coding!
Video Tutorial