title: How can I pass an array by reference to a function in C++?
body: <p>I have the following program where two variables are to be passed by reference to a function where their values will be determined based on external factors before being returned to <code>main()</code> so that they can be used by other functions. The first variable I am trying to pass is an <code>int</code>, and that goes fine, but the other is an array of strings, which is causing me some problems.</p>
  
  <p>I've done enough research into this to know that you can't have an array or references (though I've yet to figure out why) and I was wondering if anyone could help me figure out how to do this? The various methods I've tried have all resulted in <code>segmentation faults</code>.</p>
  
  <p><strong>NB</strong>: The code below has the array being passed by value since I just don't know what to write for it.</p>
  
  <p><strong>Update</strong>: I'm required to use an array for my coursework. Some other data structure, such as the <code>vector</code> that has been suggested, would be great, but I have to use specific structures.</p>
  
  <pre><code>void initialise_existing_devices(int&amp; no_of_existing_devices, string existing_devices[100]);
  
  int main()
  {
      int no_of_existing_devices = 0;
      string existing_devices[100];
  
      initialise_existing_devices(no_of_existing_devices, existing_devices[100]);
  }
  
  void initialise_existing_devices(int&amp; no_of_existing_devices, string existing_devices[100])
  {
      string line;
      ifstream DeviceList;
      DeviceList.open("devices/device_list");
      while (true)
      {
          getline(DeviceList, line, '\n');
          if (DeviceList.eof())
          {
              break;
          }
          ++ no_of_existing_devices;
      }
      DeviceList.close();
  
      DeviceList.open("devices/device_list");
      for (int i = 0; i &lt; no_of_existing_devices; i ++)
      {
          getline(DeviceList, line, '\n');
          existing_devices[i] = line;
      }
  }
  </code></pre>