summaryrefslogtreecommitdiff
path: root/example/macros.c
blob: 0e836b03e0b511b6edc2852a7356594b023f73b3 (plain)
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
#include "prb.h"
#include <stdio.h>

struct type {
	i32 i32_element;
	i16 i16_element;
	u8 u8_element;
};

#define nodes_count 10

struct stack_node {
	i32 value;
	struct stack_node *next;
};

struct stack {
	struct stack_node *first;
	struct stack_node *last;
};

static void print_stack(struct stack s)
{
	for (struct stack_node *node = s.first; node; node = node->next)
		printf("%d%s", node->value, ((node->next) ? " -> " : "\n"));
}

i32 main(void)
{
	u64 offset = offsetof(struct type, u8_element);
	assert(offset == 6);
	info("offsetof(type_t, u8_element) = %lu", offset);
	info("stack:");
	struct stack_node nodes[nodes_count];
	struct stack stack = {0};
	for (i32 i = 0; i < array_count(nodes); ++i) {
		struct stack_node *node = nodes + i;
		node->value = i;
		sllpush(stack.first, stack.last, node);
	}
	print_stack(stack);
	sllpop(stack.first, stack.last);
	print_stack(stack);
}