0

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!

3
  • 3
    This is a good time to read up on std::vector Commented Nov 13, 2015 at 17:00
  • 1
    It's not C++, it's plain old C. Forget about C. Commented Nov 13, 2015 at 17:02
  • Let me give slightly different advice: don't do this yourself at all. Use std::accumulate instead. Commented Nov 13, 2015 at 17:13

3 Answers 3

4

As a C++ beginner use std::vector for variable length arrays, and std::array for fixed size arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

I would have jumped right to std::vector, but I've heard that using that as a data type can be very inefficient when your elements aren't primitive data types. The 'sum' example I used above what just a simple example. Would you still recommend using std::vector if the elements were heavy weight objects?
0

In general I'd give the same advice, as @Cheers and hth. - Alf does.

However, I am not confident if my logic regarding the pointer and reference is on track with 'good code'.

The use of a reference isn't really needed, it would be sufficient to write

 int sum(const int* array, int lengthArray);

Use references for pointers where you want to change these from inside the function (e.g. for allocating memory).

1 Comment

I should have specified, I want my function to have access to the array elements, but I do not want the function to change the values of the elements. Does using a const in the context you did still protect the function from changing values?
-1

You could use std::vector as was suggested, but if you do need to use the C array, you can do this:

int sum(int* array, unsigned int len) 

or just

int sum(int* array)

if you use a special value (e.g INT_MAX) to make the end of the array. In C array and pointer are essentially the same thing.

3 Comments

template<size_t N> int sum(int (&array)[N]) is better, but using a std::vector or std::array is better still
@Kevin there is std::accumulate in <algorithms>, so there is no need for such a function :-)
True, but pointer vs vector vs array apply to more than just sum functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.