libcdsb/examples/array.c

38 lines
835 B
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <assert.h>
#include <stdio.h>
#include "../include/array.h"
typedef vtype_array arr_t;
int print_value(void* value, ssize_t index, vtype type, void* data) {
const char *n = data;
vtype_int32 *v = value;
assert(type == VTYPE_INT32);
printf("%s %d (index: %ld)\n", n, *v, index);
return 0;
}
int main(int argc, char** argv) {
arr_t arr;
array_init(&arr, VTYPE_INT32);
for (size_t i = 0; i < 28; ++i) {
array_push_back(&arr, i);
}
array_get_by_index(&arr, 13, "Get value:", print_value);
array_pop_by_index(&arr, 18, "Pop value:", print_value);
array_foreach(&arr, "Foreach loop:", print_value);
array_free(&arr);
}