aboutsummaryrefslogtreecommitdiff
path: root/tests/i/extern-c-struct/lib.c
blob: 45bb30eb33e857e06d5cc84eb574583e679972d3 (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

#include <stdarg.h>

struct Sample {
    char tag;
    double value;
    int count;
};

static struct Sample shared = {'G', 0.5, 7};

void scale_sample(struct Sample *s, int k) { s->value *= k; }
struct Sample *shared_sample(void) { return &shared; }
int shared_count(void) { return shared.count; }

struct Counter {
    int *target;
};

void bump(int *p) { ++*p; }
void bump_counter(struct Counter *c) { ++*c->target; }
void get_shared(struct Sample **out) { *out = &shared; }

/* Each argument after n is a struct Sample *, read back with va_arg. */
int sum_counts(int n, ...) {
    va_list ap;
    int total = 0;
    va_start(ap, n);
    for (int i = 0; i < n; ++i) total += va_arg(ap, struct Sample *)->count;
    va_end(ap);
    return total;
}