-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnestedStructExample.cpp
More file actions
70 lines (58 loc) · 1.11 KB
/
nestedStructExample.cpp
File metadata and controls
70 lines (58 loc) · 1.11 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
typedef struct value_tag
{
int x;
int y;
}value;
typedef struct st1_tag
{
unsigned int size;
value* array;
}st1;
typedef struct st2_tag
{
unsigned int size;
st1* array;
}st2;
st2 foo(int size)
{
st2 st2Struct;
st2Struct.size = size;
st2Struct.array = (st1*)malloc(size*sizeof(st1));
for(int i = 0; i < size; ++i)
{
st1 st1Struct;
st1Struct.size = size;
st1Struct.array = (value*)malloc(size*sizeof(value));
for(int j = 0; j < size; ++j)
{
value val;
val.x = j;
val.y = j+1;
st1Struct.array[j] = val;
}
st2Struct.array[i] = st1Struct;
}
return st2Struct;
}
int main()
{
st2 st = foo(10);
for(int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10; ++j)
{
printf("%d ",st.array[i].array[j].x);
}
std::endl(std::cout);
}
// free the memory
for(int i = 0; i < 10; ++i)
{
free(st.array[i].array);
}
free(st.array);
return 0;
}