for loop and pointers c++

I’ve been messing about with the for loop on c++ and its quite interesting that a copy is created for the for loop rather than editing the existing values of an array.

Here is my final code example:

#include <iostream>
#include <vector>
int main() {
    int full[]{1, 2, 3, 33, 414, 20};
    //Generally prints out the items of each int in the array
    for (auto item:full)
        std::cout << item << std::endl;

    //This code gets the reference of each item and manipulates it
    for (auto &pitem:full)
        pitem *=2;
    //reprint but also using reference
    for (auto const &item:full)
        std::cout << item << std::endl;

    return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.