Arrays can only be passed by reference, actually:
void foo(double (&bar)[10]) { }
This prevents you from doing things like:
double arr[20]; foo(arr); // won't compile
To be able to pass an arbitrary size array to foo
, make it a template and capture the size of the array at compile time:
template<typename T, size_t N> void foo(T (&bar)[N]) { // use N here }
You should seriously consider using std::vector
, or if you have a compiler that supports c++11, std::array
.
Related Posts:
- Is C++ Array passed by reference or by pointer?
- What are the differences between a pointer variable and a reference variable in C++?
- Return array in a function
- What does `*&` in a function declaration mean?
- Passing a 2D array to a C++ function
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- C++: Expression must have a constant value when declaring array inside function
- Delete 2D array C++
- initial value of reference to non-const must be an lvalue
- error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- Reference to non-static member function must be called
- C++ Array of pointers: delete or delete []?
- C++ correct way to return pointer to array from function
- Warning: comparison of distinct pointer types
- Difference between function arguments declared with & and * in C++
- Array of Linked Lists C++
- Linker Error C++ “undefined reference ” [duplicate]
- How to dynamically allocate arrays in C++
- How do I find the length of an array?
- What exactly is the difference between “pass by reference” in C and in C++?
- What does “dereferencing” a pointer mean?
- Regular cast vs. static_cast vs. dynamic_cast
- What is a smart pointer and when should I use one?
- What does “dereferencing” a pointer mean?
- What is a char*?
- Passing an array by reference
- What exactly is nullptr?
- How to convert a char array to a string?
- How to fix a “invalid operands to binary expression” error?
- How do I declare a 2d array in C++ using new?
- Pass a vector by reference C++
- lvalue required as left operand of assignment – Array
- What is uintptr_t data type
- What is the difference between const int*, const int * const, and int const *?
- how does the ampersand(&) sign work in c++?
- C++ – No matching member function for call to ‘push_back’
- What does int & mean
- What is the difference between const int*, const int * const, and int const *?
- Pointer to incomplete class type is not allowed
- Is there a function to copy an array in C/C++?
- Reverse Contents in Array
- C++ error: “Array must be initialized with a brace enclosed initializer”
- warning: ISO C++ forbids variable length array
- c++ array – expression must have a constant value
- Is the practice of returning a C++ reference variable evil?
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- When should I use the new keyword in C++?
- How to access the contents of a vector from a pointer to the vector in C++?
- How to add element to C++ array?
- How to convert vector to array
- How to find the size of an int[]?
- What does int & mean
- splitting a string into an array in C++ without using vector
- Printing an array in C++?
- C++ array assign error: invalid array assignment
- C++ Expression must have pointer-to-object type
- C++ strings and pointers
- Typedef function pointer?
- How to initialize a vector of pointers
- What is the use of intptr_t?
- How to add element to C++ array?
- Error: No instance of constructor matches the argument list
- extended initializer lists only available with
- c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)
- Creation of Dynamic Array of Dynamic Objects in C++
- how to initialize an empty integer array in c++
- C++ – Assigning null to a std::string
- Check if C++ Array is Null
- C++ Initializing a Global Array
- Comparing the values of char arrays in C++
- Using C-string gives Warning: “Address of stack memory associated with local variable returned”
- How can I assign an array from an initializer list?
- Why use pointers?
- How do I return a char array from a function?
- C++ Return value, reference, const reference
- What’s the difference between * and & in C?
- creating an array of structs in c++
- How to read lines of text from file and put them into an array
- Why can’t we pass arrays to function by value?
- Double pointer array in c++
- How to create a vector of class objects in C++?
- How to memset char array with null terminating character?
- Function stoi not declared
- creating dynamic array of string c++
- too many initializers for ‘int [0]’ c++
- Function stoi not declared
- C++ 2d char array to string
- how to find 2d array size in c++
- Is null reference possible?
- Initializing pointers in C++
- Getting error “array bound is not an integer constant before ‘]’ token”
- Using cin to input a single letter into a char
- set head to NULL (‘NULL’ : undeclared identifier)
- What is a `char*`?
- Proper way to pass dynamic arrays to other functions
- clearing a vector of pointers [duplicate]
- Meaning of *& and **& in C++
- Differences between unique_ptr and shared_ptr
- C++ float array initialization