# Thread: How to sum up all members of array ?

1. ## How to sum up all members of array ?

hi. I have written a code to calculate the total of the members of an array named "arr". It doesnt work

Im sure it is easy so please tell we where im doing wrong:

Code:
```#include <cstdlib>
#include<iostream>
using namespace std;

int summ(int *p){
int res=0;
while(*p!=NULL){
res+=*p;
p++;
cout << "res="<<res<<"   p="<<*p<<endl;
}
return res;
}
int main(int argc, char** argv) {

int arr[4]={5,1,2,6};

//    int size=sizeof(arr)/sizeof(int);
//    for(int i=0;i<size;i++){
//        arr[i] = arr[i]*2;
//    }
//
//    cout << "size = " << size<<endl;

int v=summ(arr);
cout << v;

}```

When i uncommend the part above, it works but i wanna learn how to pass a pointer in functions. So thx a lot in advance.

2. int size=sizeof(arr)/sizeof(int);
int v=summ(arr,size);

Pass the size of the array as an additional parameter.

You can't tell the size of an array inside a function just by looking at it.

3. Code:
```#include <cstddef>
#include <iostream>

template<std::size_t N>
int summ(int (&p)[N])
{
int res = 0;
for (std::size_t i = 0; i < N; i++)
res += p[i];
return res;
}

int main(int argc, char** argv)
{
int arr[] = { 5, 1, 2, 6 };
int v = summ(arr);
std::cout << v;
}```
C++ does not add anything to the end of an array, so you can't just wander through it and expect some magical value at the end. There isn't one.
Furthermore, even if there was one, it wouldn't be NULL. That's for pointers, and pointers only. Yet, you shouldn't be using NULL anyway, you should be using nullptr for pointers.
Size of the array is not needed if initialized.