title: Passing a variable into an initialization of an array as a reference in a function parameter
body: <p>I've been trying to learn C++ recently, and doing some hacker rank introductory tasks. I am attempting to do the <a href="https://www.hackerrank.com/challenges/deque-stl" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/deque-stl</a> challenge, but I am very unfamiliar with c++ syntax and uses of their base "array". If I would prefer, I would at the very least use std::array (although I understand that is nothing but a zero overhead wrapper of the default C array).</p>
  
  <p>My problem is this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;deque&gt; 
  #include &lt;vector&gt;
  #include &lt;array&gt;
  #include &lt;typeinfo&gt;
  
  using namespace std;
  void printKMax(int (&amp;arr)[n], int n, int k){
     //Write your code here.
      deque&lt;int&gt; d(arr);
      cout &lt;&lt; arr[3];
      for(int i:d) {cout &lt;&lt;i;}
  
  }
  int main(){
  
     int t;
     cin &gt;&gt; t;
     while(t&gt;0) {
        int n,k;
         cin &gt;&gt; n &gt;&gt; k;
         int i;
         int arr[n];
         for(i=0;i&lt;n;i++)
              cin &gt;&gt; arr[i];
         printKMax(arr,n, k);
         t--;
       }
       return 0;
  }
  </code></pre>
  
  <p>I am trying to pass the arr into printKMax, and receive it as a reference.
  The error I am receiving is: </p>
  
  <pre><code>!‘arr’ was not declared in this scope
  !‘n’ was not declared in this scope
  !variable or field ‘printKMax’ declared void
  !expected primary-expression before ‘int’
  !expected primary-expression before ‘int’
  </code></pre>
  
  <p>of which if I use a constant int, eg "5", arr[5], it passes through.(passing arr[5] in the function call too). But if I replace with n, it doesn't work.</p>
  
  <p>That is, at the moment, it is printKMax(arr,n,k) at the function call [not the definition], and I see no reason to add the arr[n] here (as it is already implicit in the array it is declared as: int arr[n]). I am trying to pass it as a reference, without it decaying into a pointer. I have messed around with passing it as *arr too, but with no success. </p>
  
  <p>I read online its important to keep it as (&amp;arr)[100] or something along those lines, but here I have (&amp;arr)[n], and it is out of scope. I would love to just write (&amp;arr)[], but alas I get this error instead:</p>
  
  <pre><code>parameter ‘arr’ includes reference to array of unknown bound ‘int []’
  </code></pre>
  
  <p>Thanks in advance! Also, is this only a problem with arrays? If I start using  and  and such data structures from eg the boost library, will I encounter similar problems?</p>
  
  <p>PS: ignore the deque code, I was just playing around, trying to initialise it with the same elements as the array I am passing. It is probably incompatible since one is std:: and the other is default array unfortunately...?</p>