Skip to content

Commit 41bfef6

Browse files
authored
Update Pair structure and related stack code
1 parent 091c9bc commit 41bfef6

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

_posts/2025-11-30-comptime-c-functions.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Here is how it is achieved in C:
3333

3434
GCC requires `-O1`, while Clang requires `-O2`.
3535

36-
Copy of the code on [Compiler Explorer](https://godbolt.org/z/PaoT9j1Ed):
36+
Copy of the code on [Compiler Explorer](https://godbolt.org/z/Ts5r9n73K):
3737

3838
```c
3939
#include <assert.h>
@@ -67,7 +67,6 @@ static inline ErrorCode stack_push(stack *s, const void *element) {
6767
if (s->size >= s->capacity) {
6868
return STACK_FULL;
6969
}
70-
// This memcpy() is like assigning a value of *any* type using the = operator
7170
memcpy((unsigned char *)s->data + s->size * s->element_size, element, s->element_size);
7271
s->size++;
7372
return SUCCESS;
@@ -88,27 +87,27 @@ static inline bool stack_empty(const stack *s) {
8887

8988
typedef struct {
9089
uint32_t a;
91-
uint32_t b;
92-
} Pair32;
90+
double b;
91+
} Pair;
9392

9493
int main(void) {
95-
Pair32 buffer[100];
94+
Pair buffer[100];
9695
stack s;
97-
stack_init(&s, buffer, sizeof(Pair32), 100);
96+
stack_init(&s, buffer, sizeof(Pair), 100);
9897

99-
Pair32 p1 = {.a = 10, .b = 20};
100-
Pair32 p2 = {.a = 111, .b = 222};
98+
Pair p1 = {.a = 10, .b = 20};
99+
Pair p2 = {.a = 111, .b = 222.0};
101100

102101
assert(stack_push(&s, &p1) == SUCCESS);
103102
assert(stack_push(&s, &p2) == SUCCESS);
104103

105-
Pair32 out2;
104+
Pair out2;
106105
assert(stack_pop(&s, &out2) == SUCCESS);
107-
assert(out2.a == 111 && out2.b == 222);
106+
assert(out2.a == 111 && out2.b == 222.0);
108107

109-
Pair32 out1;
108+
Pair out1;
110109
assert(stack_pop(&s, &out1) == SUCCESS);
111-
assert(out1.a == 10 && out1.b == 20);
110+
assert(out1.a == 10 && out1.b == 20.0);
112111

113112
assert(stack_empty(&s));
114113

0 commit comments

Comments
 (0)