forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferencespointers.cpp
More file actions
52 lines (40 loc) · 1.4 KB
/
Copy pathreferencespointers.cpp
File metadata and controls
52 lines (40 loc) · 1.4 KB
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
#include<iostream>
using namespace std;
// Function with integer paramter
void variableFunction(int parm) {
parm = parm * 3;
}
// Function with pointer parameter
void pointerFunction(int * parm) {
*parm = *parm * 4;
}
// Function with reference parameter
void referenceFunction(int & parm) {
parm = parm * 5;
}
int main() {
int x = 10; // integer varaible
int &ref = x; // reference variable
int * ptr = new int(15); // pointer variable
cout << " Starting: x=" << x << ", ref=" << ref <<
"*ptr=" << *ptr << ", ptr=" << ptr << endl;
// Call the 3 functions with variable x
variableFunction(x);
pointerFunction(&x);
referenceFunction(x);
cout << endl << " After calling with x: x=" << x << ", ref=" << ref <<
"*ptr=" << *ptr << ", ptr=" << ptr << endl;
// Call the 3 functions with pointer ptr
variableFunction(*ptr);
pointerFunction(ptr);
referenceFunction(*ptr);
cout << endl << " After calling with ptr: x=" << x << ", ref=" << ref <<
"*ptr=" << *ptr << ", ptr=" << ptr << endl;
// Call the 3 functions with reference ref
variableFunction(ref);
pointerFunction(&ref);
referenceFunction(ref);
cout << endl << " After calling with ptr: x=" << x << ", ref=" << ref <<
"*ptr=" << *ptr << ", ptr=" << ptr << endl;
return 0;
}