Person Struct

This is just a little question we had as part of our lab, and ive gotten everything but displaying the brother. Im not sure what I am supposed to put there. Any help would be appriciated

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
#include <iostream>
using namespace std;

struct Person {
	string firstName;
	int height;
};

void displayPerson(Person* p) {
	cout << p->firstName << endl << p->height << endl;
}

int main() {
	Person brother;
	brother.firstName = "Carl";
	brother.height = 180;

	Person* sister = new Person;
	sister->firstName = "Megan";
	sister->height = 165;
	displayPerson(_____brother);
	displayPerson(sister);

	return 0;
}
to pass brother by reference just by adding &

http://cpp.sh/7lh7
Not to be that guy, but just to clear up on terminology:
Your example is passing the address of "brother", which is passing as a pointer, not a reference.
Both use the & symbol but in somewhat different ways.

It would be a reference if passed this way:

1
2
3
4
5
6
7
8
9
void displayPerson(Person & p) {  // passed as reference
	cout << p.firstName << endl << p.height << endl;
}
int main()
{
    Person brother;
    // ...
    displayPerson(brother);
}
Last edited on
I honestly completely forgot about the &
I know the example is using it in different ways i think thats what she wants to see.
Thanks for the help!
Topic archived. No new replies allowed.