In C++ how can I pass an array as a reference when I don’t know the size at compile time? So far, I found out that the only way to make it work is to use something like
const double( &numbers ) [size]
but it means that I need to know the size of the array at compile time, thus I cannot use it in an external function.
My questions are:
- If I don’t pass an array as a
( const double( &numbers ) [length] ), because for example I don’t know its size, how do I make sure that it doesn’t get copied, but it is referenced? - If I pass an array like in the above example,
( double array[] )is it referenced or is it copied?
In C++, you should use std::vector.
In C/C++, you can’t pass arrays as a copy. Arrays are always passed by reference.
EDIT
In C++, arrays passed by reference has different meaning. In both C and C++, arrays decay into a pointer to the first element of the array. Please check the comments below.