-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoid_pointers.c
More file actions
65 lines (49 loc) · 1.21 KB
/
void_pointers.c
File metadata and controls
65 lines (49 loc) · 1.21 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
#include <stdio.h>
//#include <stdlib.h>
#include <string.h>
// Define a simple map structure
typedef struct {
const char *key;
void (*function)();
} FunctionMapEntry;
void process_start() {
printf("Process started.\n");
}
void process_data() {
printf("Processing data.\n");
}
void process_finish() {
printf("Process finished.\n");
}
// Define a function map (hash map)
FunctionMapEntry function_map[] = {
{"start", process_start},
{"data", process_data},
{"finish", process_finish}
};
// Helper function to find the function in the map
void call_function_by_key(const char *key) {
for (size_t i = 0; i < sizeof(function_map) / sizeof(FunctionMapEntry); i++) {
if (strcmp(function_map[i].key, key) == 0) {
function_map[i].function();
return;
}
}
printf("Invalid key: %s\n", key);
}
int main() {
char key[] = "data";
call_function_by_key(key); // Dynamically dispatch function based on string key
return 0;
}
// // void pointers
// #include <stddef.h>
// #include <stdint.h>
// #include <stdio.h>
// int main(){
// int a = 3;
// void *p = &a;
// char b[1] = {'z'};
// printf("%p\n", b);
// printf("%d\n", sizeof(b));
// }