-
Notifications
You must be signed in to change notification settings - Fork 0
/
testStruct.cpp
executable file
·56 lines (45 loc) · 1023 Bytes
/
testStruct.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
#include <iostream>
using namespace std;
struct inner
{
int a;
};
struct B
{
int b;
};
struct outer
{
inner x;
B y;
};
int main()
{
int value_x = 5;
int value_y = 10;
outer *out1 = (outer*)malloc(sizeof(outer));
out1->x.a = value_x;
out1->y.b = value_y;
cout << " original out1 " << endl;
cout << out1->x.a << endl;
cout << out1->y.b << endl << endl;
cout << " inner *in1 = (inner *)out1 " << endl;
inner *in1 = (inner *)out1;
cout << in1->a << endl;
in1->a = -1;
cout << out1->x.a << endl << endl;
cout << " outer *out2 = (outer *)in1 " << endl;
outer *out2 = (outer *)in1;
cout << out2->x.a << endl;
cout << out2->y.b << endl << endl;
cout << " B *i = (B *)out2 " << endl;
B *i = (B *)out2;
cout << i->b << endl;
B *j = (B *)out1;
cout << j->b << endl << endl;
cout << " inner *k = (inner *)out1; " << endl;
inner *k = (inner *)out1;
B *l = (B *)out1;
cout << k << endl;
cout << l << endl;
}