I am confused about the pointer when passing to functions in C++, when put *arr in the parameter(line 3), is it means int *arr = balance
, I know balance can refer the first element address in the array, but the problem is why arr[i] refers the first element value?
I think *arr means value, while arr means address...
#include <iostream>
using namespace std;
double getAverage(int *arr, int size);
int main(){
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
avg = getAverage(balance, 5);
cout << avg << endl;
return 0;
}
double getAverage(int *arr, int size){
int i, sum = 0;
double avg;
for (i = 0; i< size; i++){
sum += arr[i]; //in this line, I think it should sum += *arr[i], arr means a address, isn't?
}
avg = double(sum) / size;
return avg;
}