blob: 3de12e40aa875c2343156d8b5d94de4250672021 (
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
45
46
47
48
|
#include <stdio.h>
#include <stdlib.h>
#include "utarray.h"
typedef struct {
int a;
char *s;
} intchar_t;
static void intchar_copy(void *_dst, const void *_src)
{
intchar_t *dst = (intchar_t*)_dst;
const intchar_t *src = (const intchar_t*)_src;
dst->a = src->a;
dst->s = (src->s != NULL) ? strdup(src->s) : NULL;
}
static void intchar_dtor(void *_elt)
{
intchar_t *elt = (intchar_t*)_elt;
if (elt->s != NULL) {
free(elt->s);
}
}
int main()
{
UT_array *intchars;
intchar_t ic, *p;
UT_icd intchar_icd = {sizeof(intchar_t), NULL, intchar_copy, intchar_dtor};
utarray_new(intchars, &intchar_icd);
ic.a=1;
ic.s=(char*)"hello";
utarray_push_back(intchars, &ic);
ic.a=2;
ic.s=(char*)"world";
utarray_push_back(intchars, &ic);
p=NULL;
while( (p=(intchar_t*)utarray_next(intchars,p)) != NULL ) {
printf("%d %s\n", p->a, (p->s != NULL) ? p->s : "null");
}
utarray_free(intchars);
return 0;
}
|