How to pass an array by reference if the data type is a typedef. I am learning c++, I read concepts of call-by-reference, but when I implemented according to that - I am getting an error(pasted below after the code). Please, can anyone explain the best way to send an array to function as a call by reference?
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long ulong;
ulong fib_dynamic(ulong n, ulong &memo[]){
if(n < 2) return 1;
if(memo[n] == 0){
memo[n] = fib_dynamic(n-1, memo) + fib_dynamic(n-2, memo);
}
return memo[n];
}
ulong fib_iterative(ulong n){
ulong fib[n+1];
fib[0] = 1;
fib[1] = 1;
for(int i=2; i<n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[n-1];
}
int main(){
ulong n;
cout << "Welcome to Fib Calculator\nEnter the n:";
cin >> n;
ulong memo[n];
cout << endl << n << " th fib num(dynamic) = " << fib_dynamic(n,memo) << endl;
}
// error
1-fib-dp.cpp:13:47: error: 'memo' declared as array of references of type
'unsigned long &'
ulong fib_dynamic(ulong n, unsigned long &memo[]){
^
1-fib-dp.cpp:37:53: error: no matching function for call to 'fib_dynamic'
cout << endl << n << " th fib num(dynamic) = " << fib_dynamic(n,memo) << endl;
^~~~~~~~~~~
1-fib-dp.cpp:13:7: note: candidate function not viable: no known conversion from
'ulong [n]' to 'int' for 2nd argument
ulong fib_dynamic(ulong n, unsigned long &memo[]){
^
2 errors generated.