Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3437306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:02:56+00:00 2026-05-18T08:02:56+00:00

I have the following program where two variables are to be passed by reference

  • 0

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 main() so that they can be used by other functions. The first variable I am trying to pass is an int, and that goes fine, but the other is an array of strings, which is causing me some problems.

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 segmentation faults.

NB: The code below has the array being passed by value since I just don’t know what to write for it.

Update: I’m required to use an array for my coursework. Some other data structure, such as the vector that has been suggested, would be great, but I have to use specific structures.

void initialise_existing_devices(int& 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& 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 < no_of_existing_devices; i ++)
    {
        getline(DeviceList, line, '\n');
        existing_devices[i] = line;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:02:57+00:00Added an answer on May 18, 2026 at 8:02 am

    A reference to an array looks like:

    void f(std::string (&a)[N]) { }
    

    where a is the name of the parameter and N is the number of elements in the array.


    However, usually in C++ you don’t pass an array by reference (you can; it’s just not common). Other options include:

    • Pass a pointer to the initial element of the array; in this case, consider passing the size of the array as a second argument to the function.

    • Use a std::vector<std::string> or a std::array<std::string, N> instead and pass it by reference (you can also find the array psuedo-container in Boost; barring that, consider writing your own. If you take a look at the Boost source code, it’s quite simple and straightforward).

    • Pass a pair of iterators (begin and end) to the function and use them to manipulate the range.

    The last option is the most idiomatic C++ approach; it is also the most generic because you can use any type of container, including arrays, standard library containers, or containers that you’ve written yourself.


    Since you are actually trying to use the parameter as an “out” parameter, it’s probably better just to return a std::vector<string> or a std::array<string, 100> containing the results; this is much cleaner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following program: ~/test> cat test.cc int main() { int i =
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
let us have a situation in which the following program prints some 10 lines
I have the following layout for my test suite: TestSuite1.cmd: Run my program Check
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have a Ruby program that loads up two very large yaml files, so
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.