-
Notifications
You must be signed in to change notification settings - Fork 0
/
cx.c
89 lines (76 loc) · 1.51 KB
/
cx.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// HEADER
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INT_TYPE 1
#define STR_TYPE 4
#define TUPLE_TYPE 7
#define MAXCHAR 40
typedef struct {
int type;
union{
int i;
char s[MAXCHAR+1];
void *ptr;
};
} Any;
Any Int(int);
Any Str(char*);
Any Tuple(Any, Any);
void toStr(Any);
// end HEADER
int main (){
Any a2, a3, a4;
a2 = Int(4);
a3 = Int(5);
a4 = Tuple( a2, a3);
// current state of a2, pointed to by a4.ptr[0]
printf("\n\n");
Any* x_address = ((Any**)a4.ptr)[0];
printf ("x address is %d\n", x_address);
printf ("x is %d\n", x_address->i);
//==========================================
// the method pass
//==========================================
printf("-----enter method-----\n");
toStr ( a4 );
printf("\n\n\n");
return;
}
//==============================================
// method being passed to
// =============================================
void toStr(Any a){
Any* x_address = ((Any**)a.ptr)[0];
printf ("x address is %d\n", x_address);
printf ("x is %d\n", x_address->i);
}
//=============================================
// Constructors
//=============================================
Any Int(int i)
{
Any a;
a.i = i;
a.type = INT_TYPE;
return a;
}
Any Str(char* s)
{
Any a;
strcpy(a.s, s);
a.type = STR_TYPE;
return a;
}
Any Tuple(Any a, Any b)
{
Any d;
d.type = TUPLE_TYPE;
d.ptr = malloc( sizeof(d.ptr) * 2 );
Any** any = (Any**)d.ptr;
static Any x; x = a;
static Any y; y = b;
any[0] = &x;
any[1] = &y;
return d;
}