Home > Net >  I met some problems with using ranged based loops to calculate sum of array by using pointers and fu
I met some problems with using ranged based loops to calculate sum of array by using pointers and fu

Time:10-15

Everything was ok until I passed the array as a pointer into the "total" function.

An error occured after I tried to used auto i to calculate sum of array -

int total = 0;

for(auto i : *arr) {
  total =i;
}

This is my output

    // This is my codes
    #include <iostream>
    
    using namespace std;
    
    int total(int *);
    
    int main () {
        
      int arr[] = {1, 2, 3, 4, 5};
    
      cout << "Displaying array:\n";
    
      for(auto i : arr) {
        cout << i << " ";
      } 
      
      cout << "\n\nThe sum of array: " << total(arr);
        
      return 0;
    }
    
    int total(int *arr) {
      int total = 0;
      
      for(auto i : *arr) {   //where the error occured
        total =i;
      }  
      
      return total;
    }

CodePudding user response:

int total(int *arr)

The parameter to this function is a pointer to an integer. arr is an int *, nothing more, nothing else. That's what the above declaration means in C .

    for(auto i : *arr) {   //where the error occured

And the error is because range iteration is defined only for containers or other objects for which std::begin and std::end is specialized.

Neither int (what *arr resolves to), nor int *, meets those requirements. Hence the compilation error.

It is true that an array was passed into this function, for the arr parameter. This is true, but irrelevant. In this function this is only an int *, a single, lonely, boring pointer to an integer. Nothing more, nothing else.

CodePudding user response:

As soon as you supply an array as an argument to a function, the array decays into a pointer to the first element in the array.

In here, this arr has no knowledge about the extent of the arr[5] you passed as an argument:

int total(int *arr) {
    // arr is a pointer to a single `int`
}

Here are two versions preserving the knowledge about the extent of the array.

  1. A pointer to an int[5]:
#include <iostream>

int total(int (*)[5]);

int main() {
    int arr[] = {1, 2, 3, 4, 5};

    std::cout << "Displaying array:\n";

    for (auto i : arr) {
        std::cout << i << " ";
    }

    std::cout << "\n\nThe sum of array: " << total(&arr); // & takes its address

    return 0;
}

int total(int (*arr)[5]) {
    int total = 0;

    for (auto i : *arr) {  // derefrence the pointer to get an int[5]
        total  = i;
    }

    return total;
}
  1. A reference to an int[5]:
#include <iostream>

int total(int (&)[5]);

int main() {
    int arr[] = {1, 2, 3, 4, 5};

    std::cout << "Displaying array:\n";

    for (auto i : arr) {
        std::cout << i << " ";
    }

    std::cout << "\n\nThe sum of array: " << total(arr);

    return 0;
}

int total(int (&arr)[5]) {
    int total = 0;

    for (auto i : arr) { // arr is a reference to an int[5]
        total  = i;
    }

    return total;
}
  • Related