libcdsb/examples/array.c

38 lines
846 B
C
Raw Permalink Normal View History

2022-08-16 21:11:07 +03:00
/* 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"
2022-08-16 21:11:07 +03:00
typedef vtype_array arr_t;
2023-03-23 18:18:51 +03:00
int print_value(vtype_variable value, ssize_t index, void* data) {
2022-08-16 21:11:07 +03:00
const char *n = data;
2023-03-23 18:18:51 +03:00
vtype_int32 *v = value.pointer;
2022-08-16 21:11:07 +03:00
2023-03-23 18:18:51 +03:00
assert(value.type == VTYPE_INT32);
2022-08-16 21:11:07 +03:00
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);
}