-2

So, I have this function that I wrote about a month ago and forgot exactly how it works.

int readFile(int racerId_func[30], double raceData_func[30][8]) {
    string fileLoc;
    ifstream inFile;
    int racersNum_func;
    cout << "Enter an input filepath: "; // Type in the filepath
    getline(cin, fileLoc);
    inFile.open(fileLoc);

    if (inFile.fail()) {
        cout << "Error: Could not open file." << endl;
        exit(1);
    }

    inFile >> racersNum_func; // Gets the number of racers

    for (int i=0; i < racersNum_func; i++) {
        inFile >> racerId_func[i]; // Assigns each racer's ID number
        for (int j=0; j < 8; j++) {
            inFile >> raceData_func[i][j];
        }
    }
    return racersNum_func;
}

This is supposed to read from a file the number of racers, then the ID and lap times of each racer. Notice that it only returns racersNum. Now, I call upon this function my main function like so:

int racersNum, racersQual; // vars included for reference
int racersDis = 0;
int racerId[30];
double raceData[30][8];
double average[30];

racersNum = readFile(racerId, raceData); // I call it right here

Somehow, racerId and raceData are also passed back from the function, even though I only returned racersNum. Why is this? Do I have a fundamental misunderstanding of how return works? Do you need to see my whole program?

Andrew Pampuch
  • 65
  • 1
  • 10
  • 1
    you send it in as pointers, so the function can (and do) modify them – sp2danny Dec 11 '14 at 01:30
  • 3
    This has nothing to do with `return`, it has to do with argument passing. When you use an array as an argument, it doesn't pass a copy of the array, it converts the array to a pointer and passes that. So your function is modifying the arrays in place. – Barmar Dec 11 '14 at 01:31
  • 3
    I hope you've learnt your lesson: _write documentation_. – Lightness Races in Orbit Dec 11 '14 at 01:38
  • @Barmar: That's the _answer_ (and a very well-worded one). Would you care to post it as such, instead of as a _comment_, where it could disappear forever at any time without warning? – Lightness Races in Orbit Dec 11 '14 at 01:39
  • @Barmar in modern C++ using C-style arrays is mostly inadvisable, so a book might not cover it – M.M Dec 11 '14 at 01:39
  • @LightnessRacesinOrbit There are already 4 answers that say essentially the same thing. – Barmar Dec 11 '14 at 01:41
  • @Barmar: Yet, at the time you posted that comment, there were _zero_ answers. Why did you write an answer as a comment? :( – Lightness Races in Orbit Dec 11 '14 at 01:42
  • @MattMcNabb Textbooks and tutorials are typically way out of date. Considering the number of C++ questions that are about C-style arrays, I doubt most of them have switched over to vectors. – Barmar Dec 11 '14 at 01:42

4 Answers4

1

That is because racerId_func and raceData_func act as pointers to the actual data. And inside the function you are modifying that data.

If you modify the data the pointers are pointing to, then outside the function you will see the data modified.

This is one of the ways you can make a function "return" more than one value. But note that returning data and modifying data through the parameters are two different things.

Daniel
  • 21,933
  • 14
  • 72
  • 101
1

When you mention that racerID and raceData are returned you probably mean that the content is modified in the readFile function and you did not expect it to.

Basically this is the result that in C/C++ arrays are pointers. So when you pass the arrays to the functions they are pointers and any change that you make inside the readFile function will also persist in the arrays after you exit the function.

uncletall
  • 6,609
  • 1
  • 27
  • 52
1

You're passing in a pointer int racerId_func[30] as a parameter is effectively int* racerId_func and pointers are mutable. If you were to change those parameters to const int racerId_func[30] saying "I don't expect these things to be modified" you'd fail to compile because the code inFile >> racerId_func[i] is writing to them.

Charlie
  • 1,487
  • 10
  • 9
1

If you declare a function parameter with C-style array type, then actually the function must be called with a pointer to the first element of an array of that type. Link to rationale.

So in effect, C-style arrays get passed by reference whereas non-arrays are passed by value. This is why changes to the arrays in the function show up in main.

If you want to really pass an array by value, you will have to wrap it in a struct. The standard library has such a wrapper, it's called std::array.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365