-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers_to_structures.cpp
67 lines (54 loc) · 1.33 KB
/
pointers_to_structures.cpp
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
#include <iostream>
using namespace std;
enum gender
{
m = 1,
f,
u
};
struct PersonalDetails
{
string fName = "";
string lName = "";
int age = -1;
gender gender = u;
int bank_bal;
int *bank_bal_pointer = NULL;
};
void display(PersonalDetails *pointer);
void update(PersonalDetails personalDetails);
void updateUsingPointer(PersonalDetails *personalDetails);
int main()
{
PersonalDetails p;
p.fName = "Aryan";
p.lName = "Jain";
p.age = 24;
p.gender = m;
p.bank_bal = 1000;
p.bank_bal_pointer = &p.bank_bal;
// display(&p);
cout << "Name before: " << p.fName << endl;
// update(p); // wont change
updateUsingPointer(&p);
cout << "Name after: " << p.fName << endl;
cout << "Bank bal before: " << p.bank_bal << endl;
update(p); // using pointer member
cout << "Bank bal after: " << p.bank_bal << endl;
return 1;
}
void display(PersonalDetails *pointer)
{
cout << "First Name: " << (*pointer).fName << endl;
cout << "Last Name: " << pointer->lName << endl;
}
void update(PersonalDetails personalDetails)
{
personalDetails.fName = "Name change";
// personalDetails.bank_bal = 2000;
*(personalDetails.bank_bal_pointer) = 2000;
}
void updateUsingPointer(PersonalDetails *personalDetails)
{
personalDetails->fName = "Aryan Jain";
}