I'm currently working on a function that I wish to accept various array lengths as inputs. For example, let's say I have a function called 'sum'. Ideally, I would want to feed this function an array of ints that can vary in size (i.e. one call will sum [1, 2, 3, 4] and another call may sum [1, 1, 1].
What is the best practice for achieving this? I had attempted declaring a function as
int sum(int array[]);
but this throws a compiler error saying that the length of the argument array must be specified.
Would it be better practice to use a reference to a pointer titled array along with a length specifier that would be used as iterator? As in
int sum(const int*& array, int lengthArray);
With this approach, I assume it would be possible to get access to the values of the array, and then be able to cycle through elements using the length of the array as another argument. However, I am not confident if my logic regarding the pointer and reference is on track with 'good code'.
I don't have the means to try this until I get home from work, but the problem has been on my mind ever since it came up. So I thought I would pose the question regarding what the best practice is.
Any help is appreciated!
std::accumulateinstead.