-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoidpointer.c
More file actions
82 lines (66 loc) · 1.4 KB
/
voidpointer.c
File metadata and controls
82 lines (66 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
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
#include <stdio.h>
#include <stdlib.h>
typedef enum SnekObjectKind {
INTEGER,
FLOAT,
BOOL,
} snek_object_kind_t;
typedef struct SnekInt {
char *name;
int value;
} snek_int_t;
typedef struct SnekFloat {
char *name;
long _pad;
float value;
} snek_float_t;
typedef struct SnekBool {
char *name;
long _pad;
unsigned int value;
} snek_bool_t;
void snek_zero_out(void *pointer, snek_object_kind_t kind)
{
if(kind == INTEGER)
{
snek_int_t *obj = (snek_int_t *)pointer;
obj->value = 0;
}
if(kind == FLOAT)
{
snek_float_t *obj = (snek_float_t *)pointer;
obj->value = 0.0;
}
if(kind == BOOL)
{
snek_bool_t *obj = (snek_bool_t *)pointer;
obj->value = 0;
}
}
int main()
{
printf("a simple example of a use of `void*`\n");
snek_int_t integer;
snek_float_t float_num;
snek_bool_t boolean;
integer.value = -100;
float_num.value = -99.99;
boolean.value = 255;
snek_zero_out(&integer, INTEGER);
snek_zero_out(&float_num, FLOAT);
snek_zero_out(&boolean, BOOL);
if (integer.value != 0)
{
printf("Integer should be zeroed out to 0\n");
}
if (float_num.value != 0.00)
{
printf("Float should be zeroed out to 0.00\n");
}
if (boolean.value != 0)
{
printf("Boolean should be zeroed out to 0\n");
}
printf("done!\n");
return 0;
}