|
diff --git a/examples/array.c b/examples/array.c
|
| ... |
| 86 |
|
86 |
|
| 87 |
printf(" Using array_foreach: "); |
87 |
printf(" Using array_foreach: "); |
| 88 |
int val; |
88 |
int val; |
| 89 |
array_foreach(values, val) { printf("%d ", val); } |
89 |
array_foreach(values, val) { |
|
|
90 |
printf("%d ", val); |
|
|
91 |
} |
| 90 |
printf("\n"); |
92 |
printf("\n"); |
| 91 |
|
93 |
|
| 92 |
printf(" Using array_foreach_i: "); |
94 |
printf(" Using array_foreach_i: "); |
| 93 |
array_foreach_i(values, val, idx) { printf("[%zu]=%d ", idx, val); } |
95 |
array_foreach_i(values, val, idx) { |
|
|
96 |
printf("[%zu]=%d ", idx, val); |
|
|
97 |
} |
| 94 |
printf("\n\n"); |
98 |
printf("\n\n"); |
| 95 |
|
99 |
|
| 96 |
array_free(values); |
100 |
array_free(values); |
| ... |
| 107 |
|
111 |
|
| 108 |
printf(" Words: "); |
112 |
printf(" Words: "); |
| 109 |
const char *word; |
113 |
const char *word; |
| 110 |
array_foreach(words, word) { printf("%s ", word); } |
114 |
array_foreach(words, word) { |
|
|
115 |
printf("%s ", word); |
|
|
116 |
} |
| 111 |
printf("\n\n"); |
117 |
printf("\n\n"); |
| 112 |
|
118 |
|
| 113 |
array_free(words); |
119 |
array_free(words); |
| ... |
| 123 |
|
129 |
|
| 124 |
printf(" People:\n"); |
130 |
printf(" People:\n"); |
| 125 |
Person person; |
131 |
Person person; |
| 126 |
array_foreach(people, person) { printf(" ID: %d, Name: %s\n", person.id, person.name); } |
132 |
array_foreach(people, person) { |
|
|
133 |
printf(" ID: %d, Name: %s\n", person.id, person.name); |
|
|
134 |
} |
| 127 |
printf("\n"); |
135 |
printf("\n"); |
| 128 |
|
136 |
|
| 129 |
array_free(people); |
137 |
array_free(people); |
| ... |
|
diff --git a/examples/foreach.c b/examples/foreach.c
|
| ... |
| 8 |
int numbers[] = {10, 20, 30, 40, 50}; |
8 |
int numbers[] = {10, 20, 30, 40, 50}; |
| 9 |
int num; // Declare the loop variable |
9 |
int num; // Declare the loop variable |
| 10 |
|
10 |
|
| 11 |
static_foreach(int, num, numbers) { printf(" Number: %d\n", num); } |
11 |
static_foreach(int, num, numbers) { |
|
|
12 |
printf(" Number: %d\n", num); |
|
|
13 |
} |
| 12 |
printf("\n"); |
14 |
printf("\n"); |
| 13 |
|
15 |
|
| 14 |
// Example 2: Iterate over an array of floats |
16 |
// Example 2: Iterate over an array of floats |
| 15 |
float prices[] = {9.99f, 19.99f, 29.99f, 49.99f}; |
17 |
float prices[] = {9.99f, 19.99f, 29.99f, 49.99f}; |
| 16 |
float price; // Declare the loop variable |
18 |
float price; // Declare the loop variable |
| 17 |
|
19 |
|
| 18 |
static_foreach(float, price, prices) { printf(" Price: $%.2f\n", price); } |
20 |
static_foreach(float, price, prices) { |
|
|
21 |
printf(" Price: $%.2f\n", price); |
|
|
22 |
} |
| 19 |
printf("\n"); |
23 |
printf("\n"); |
| 20 |
|
24 |
|
| 21 |
// Example 3: Iterate over an array of strings |
25 |
// Example 3: Iterate over an array of strings |
| 22 |
const char *fruits[] = {"Apple", "Banana", "Cherry", "Date", "Elderberry"}; |
26 |
const char *fruits[] = {"Apple", "Banana", "Cherry", "Date", "Elderberry"}; |
| 23 |
const char *fruit; // Declare the loop variable |
27 |
const char *fruit; // Declare the loop variable |
| 24 |
|
28 |
|
| 25 |
static_foreach(const char *, fruit, fruits) { printf(" Fruit: %s\n", fruit); } |
29 |
static_foreach(const char *, fruit, fruits) { |
|
|
30 |
printf(" Fruit: %s\n", fruit); |
|
|
31 |
} |
| 26 |
printf("\n"); |
32 |
printf("\n"); |
| 27 |
|
33 |
|
| 28 |
// Example 4: Perform calculations with static_foreach |
34 |
// Example 4: Perform calculations with static_foreach |
| ... |
| 30 |
int sum = 0; |
36 |
int sum = 0; |
| 31 |
int val; // Declare the loop variable |
37 |
int val; // Declare the loop variable |
| 32 |
|
38 |
|
| 33 |
static_foreach(int, val, values) { sum += val; } |
39 |
static_foreach(int, val, values) { |
|
|
40 |
sum += val; |
|
|
41 |
} |
| 34 |
printf(" Sum of values: %d\n", sum); |
42 |
printf(" Sum of values: %d\n", sum); |
| 35 |
printf("\n"); |
43 |
printf("\n"); |
| 36 |
|
44 |
|
| ... |
| 43 |
Person people[] = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 35}}; |
51 |
Person people[] = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 35}}; |
| 44 |
Person person; // Declare the loop variable |
52 |
Person person; // Declare the loop variable |
| 45 |
|
53 |
|
| 46 |
static_foreach(Person, person, people) { printf(" %s is %d years old\n", person.name, person.age); } |
54 |
static_foreach(Person, person, people) { |
|
|
55 |
printf(" %s is %d years old\n", person.name, person.age); |
|
|
56 |
} |
| 47 |
printf("\n"); |
57 |
printf("\n"); |
| 48 |
|
58 |
|
| 49 |
// Example 6: Modify array elements in place |
59 |
// Example 6: Modify array elements in place |
| ... |
| 51 |
int n; // Declare the loop variable |
61 |
int n; // Declare the loop variable |
| 52 |
|
62 |
|
| 53 |
printf(" Before: "); |
63 |
printf(" Before: "); |
| 54 |
static_foreach(int, n, nums) { printf("%d ", n); } |
64 |
static_foreach(int, n, nums) { |
|
|
65 |
printf("%d ", n); |
|
|
66 |
} |
| 55 |
printf("\n"); |
67 |
printf("\n"); |
| 56 |
|
68 |
|
| 57 |
// Note: static_foreach creates a copy of each element by default |
69 |
// Note: static_foreach creates a copy of each element by default |
| ... |
| 61 |
} |
73 |
} |
| 62 |
|
74 |
|
| 63 |
printf(" After doubling: "); |
75 |
printf(" After doubling: "); |
| 64 |
static_foreach(int, n, nums) { printf("%d ", n); } |
76 |
static_foreach(int, n, nums) { |
|
|
77 |
printf("%d ", n); |
|
|
78 |
} |
| 65 |
printf("\n"); |
79 |
printf("\n"); |
| 66 |
|
80 |
|
| 67 |
return 0; |
81 |
return 0; |
| ... |