Understanding function definitions with Pointer parameters

Can someone help me understand coding function definition for a prototype that includes pointers and arrays? After defining this function I want to print the sum of all the elements of the array. is my definition even close? compilers are not allowed at work computers so I can't test it.

void outputSomething(double *, const int*) first parameter is pointer to array
and second is pointer to the size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    void outputSomething(double *, const int*)
{
    
    double anyArray[];
    //Array size
    const double ARRAY_SIZE = 10;
    int sum = 0;
   //for loop
   for(i = 0; i < ARRAY_SIZE; i++)
      {
        sum = sum + anyArray[i];
        cout<<sum<<endl;
      }
    }
First, you'll want to name the parameters to your function. Otherwise, they may as well not even exist as you'll have no way to refer to them - and you definitely want to actually look in the array to sum it.

Your current code will not work as you are trying to sum anyArray (rather than the input array) which hasn't even been allocated or initialized. You are also using an (effectively) arbitrary constant ARRAY_SIZE, when you should be using the size that was passed in.

The summing line itself will do as long as you change it to refer to the array you pass in, and you will very likely want to move the printing of the sum outside of the loop unless you intentionally want to see the value accumulate in real time.
Topic archived. No new replies allowed.