-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvariables_pointers.cpp
More file actions
50 lines (36 loc) · 961 Bytes
/
variables_pointers.cpp
File metadata and controls
50 lines (36 loc) · 961 Bytes
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
#include <cstdio>
#include <iostream>
using namespace std;
struct myStruct
{
int x;
bool y;
double z;
};
int main(){
myStruct* x = nullptr;
myStruct* y = new myStruct();
myStruct* z;
y->x = 20;
// copies the object into stack
myStruct a = *y;
a.x = 10;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "z: " << z << endl;
cout << "Size: " << sizeof(myStruct*) << endl;
cout << "x - y = " << (x - y) << endl;
cout << "y - z = " << (y - z) << endl;
cout << "&y = " << &y << endl;
cout << "y value = " << y->x << endl;
cout << "a value = " << a.x << endl;
// Creates a reference variable.
// Note the & after the data type.
// It doesn't copy the object.
// It only copies the memory address.
myStruct& b = *y;
b.x = 30;
cout << "y value = " << y->x << endl;
cout << "b value = " << b.x << endl;
return 0;
}