So, I was told to... "Write a function Adder() that receives a pointer to an integer array as input, and uses this pointer to return the sum of elements of the array." And I was pretty successful. My code is
#include <bits/stdc++.h>
using namespace std;
int Adder (int *ptr)
{
int sum=0;
for (int i=0; i<5; i++)
{
sum=*(ptr+i)+sum;
}
return sum;
}
int main(){
int array[5]={1,1,1,1,1};
int sum;
int *ptr=array;
Adder(ptr);
sum=Adder(ptr);
cout<<sum;
}
The thing I can't understand is where I
Adder(ptr)
and then
int Adder (int *ptr)
"ptr" holds the address, right? While, " *ptr " holds the actual value. I can't understand how this worked. Can someone please explain?