dimanche 2 mai 2021

what is the purpose of using &n in the following code? [duplicate]

why do we use &n in the following code? I did some research and found that &n uses the memory address of the array elements to access their data without copying them to a new variable. This is memory-efficient. But I don't completely understand it.

  #include <iostream>
    using namespace std;
    int main() {
        int numbers[5] = {7, 5, 6, 12, 35};
    
        cout << "The numbers are: ";
    
        //  Printing array elements
        // using range based for loop
        for (const int &n : numbers) {
            cout << n << "  ";
        }

Aucun commentaire:

Enregistrer un commentaire