Classes in Functions

Why is it then when an object is passed to a function in "main" that its members change in "main" and not just in the function. I'll copy my code and explain better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;


//Person - stores the name and SSN
class Person
{
    public:
        char szFirstName[128];
        char szLastName[128];
        int nSocialSecurityNumber;
};


// getPerson - read a Person object from the keyboard
//              and return a copy to the caller
Person getPerson()
{
    Person p2erson;

    cout << "\nEnger another Person\n" << "First name: ";
    cin >> p2erson.szFirstName;

    cout << "Last Name: ";
    cin >> p2erson.szLastName;

    cout << "SSN: ";
    cin >> p2erson.nSocialSecurityNumber;

    return p2erson;

}

// getPeople - read array of Person objects; return the number read
int getPeople (Person p1eople[], int nMaxSize)
{
    int index;
    for (index = 0; index < nMaxSize; index++)
    {
        char cAnswer;
        cout << "Enter another name? (Y or N): ";
        cin >> cAnswer;

        if (cAnswer != 'Y' && cAnswer != 'y')
        {
            break;
        }
        p1eople[index] = getPerson();
    }
    return index;
}

// displayPerson - display a person on the default display
void displayPerson(Person p3erson)
{
    cout << "First name: " << p3erson.szFirstName << endl;
    cout << "Last name: " << p3erson.szLastName << endl;
    cout << "SSN: " << p3erson.nSocialSecurityNumber << endl;
}

//displayPeople - display an array of Person objects
void displayPeople (Person p4eople[], int nCount)
{
    for (int index = 0; index < nCount; index++)
    {
        displayPerson(p4eople[index]);
    }
}


//sortPeople - sort an array of nCount Person objects by SSN
void sortPeople(Person p5eople[], int nCount)
{
    int nSwaps = 1;
    while(nSwaps != 0)
    {
        nSwaps = 0;
        for(int n = 0; n < (nCount -1); n++)
        {
            if(p5eople[n].nSocialSecurityNumber > p5eople[n+1].nSocialSecurityNumber)
            {
                Person temp = p5eople[n+1];
                p5eople[n+1] = p5eople [n];
                p5eople[n] = temp;

                nSwaps++;
            }
        }
    }
}

int main(int nNumberofArgs, char* pszArgs[])
{
    Person people[128];

    cout << "Read name/ssn info\n";
    int nCount = getPeople(people, 128);
    cout << "\nHere is person 1: " << people[0].nSocialSecurityNumber << endl;


    sortPeople(people, nCount);

    cout << "\nHere is the list sorted by SSN: " << endl;
    displayPeople(people, nCount);

    system("PAUSE");
    return 0;
}


My question moreless pertains to LINE 99. See, when getPeople is called, I understand that 'people' and '128' are passed to the function. What I do not understand is how anything that happens within the functions will alter 'people' within the context of "main". I thought that this line was simply using the function(s) to get a value for nCount and that any values passed were essentially independent of 'people' within "main". Sorry if this is confusing, I'm working through a C++ book and ran into this. Thanks so much for any help.
Have you already read about call by reference? Arrays are actually pointers, so in int nCount = getPeople(people, 128); people is a pointer to a memory region.
OK, so essentially any of the functions that have "people" passed to them will be altering whatever is stored at the address that "people" points to then....and that is why it is changing within "main"?

I was only confused because previously I had been passing int's to functions, and the int variable passed to a function was not changed within "main".
Ah, so you don't know the difference between call by reference and call by value yet?

Alright, let's compare these two functions:

1
2
3
4
5
6
7
8
9
10
11

void callByValue(int value)
{
     value = 5;
     return 0;
}

void callByReference(char[] cstring)
{
     cstring[0] = 'a';
}


The first function will do absolutely nothing, whereas the second one will actually change the array passed to it (oh well, or crash your program under some circumstances).

The difference is what is passed through the function:
in callByValue, we pass an int. But we are passing the value of the int, not the int variable itself. That also goes for arrays. However, arrays are implicitly pointers to memory regions - which means they are basically numbers that tell you when in the memory their data is located. Which means if you pass an array to a function, you don't really pass the array, you pass the location of the array. This allows getPeople to operate on the actual data instead of on copies of it.
OK, that makes sense. So really anytime you are passing an array to a Function you are calling by reference then. Thanks a lot, it makes sense to me now. It is much appreciated and I hope to one day be good enough to help out other noobs like myself!
Topic archived. No new replies allowed.