0

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.
Mário Feroldi
  • 3,463
  • 2
  • 24
  • 49
ajayramesh
  • 3,576
  • 8
  • 50
  • 75
  • Why do you feel you need a reference in the first place? Make it `ulong memo[]` – Igor Tandetnik Jun 16 '17 at 03:50
  • Note also that `ulong memo[n]` has `n` elements, addressed with indexes `0` through `n-1`. So `memo[n]` exhibits undefined behavior by way of a buffer overrun. – Igor Tandetnik Jun 16 '17 at 03:52
  • `ulong &memo[]` is an array (pointer) to references of `ulong`s (`ulong&`). `ulong (&memo)[]` is a reference to an array of `ulong`s. – Mário Feroldi Jun 16 '17 at 03:53

2 Answers2

4

Use a std::vector since this is a situation where you want a dynamically sized array (i.e. the size of your array is decided at runtime). Pass the vector by reference and be careful to not go out of bounds

#include <iostream>
#include <vector>
using namespace std;

ulong fib_dynamic(ulong n, std::vector<unsigned long>& 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];
}

int main() {

    ulong n;
    cout << "Welcome to Fib Calculator\nEnter the n:";
    cin >> n;
    std::vector<unsigned long> memo(n + 1);

    cout << endl << n << " th fib num(dynamic) = " << fib_dynamic(n,memo)
         << endl;
}
Curious
  • 20,870
  • 8
  • 61
  • 146
0

Arrays are default passed by pointers. Read this and this.

Code -

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long ulong;


ulong fib_dynamic(ulong n, ulong (*memo)){
  if(n < 2) return 1;
  if(memo[n]!=0)return memo[n];
  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(ulong 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+1];
  memset(memo,0,sizeof memo);
  memo[0]=memo[1]=1;
  cout << endl << n << " th fib num(dynamic) = " << fib_dynamic(n,memo) << endl;
}

I made a few modifications as it wasn't working correctly.

Polish
  • 554
  • 1
  • 4
  • 18