A generic dynamic array (vector) implementation in C.
This project was built as a learning exercise to better understand memory layout, pointer arithmetic, and how dynamic containers work internally.
- Generic storage using
void* - Automatic resizing
- Push, pop and reserve operations
- Remove elements by index
- Custom element printing via function pointers
#include "vector.h"
void print_int(void *p) {
int *x = (int*)p;
printf("%d ", *x);
}
bool search_number(void* a, void* b) {
return *(int*)a == *(int*)b;
}
int main() {
Vector v;
vector_init(&v, 2, sizeof(int));
VECTOR_PUSH(&v, int, 10);
VECTOR_PUSH(&v, int, 20);
VECTOR_INSERT(&v, int, 99, 1);
VECTOR_SET(&v, int, 100, 0);
for (size_t i = 0; i < v.size; i++) {
printf("v[%zu] = %d\n", i, VECTOR_AT(&v, int, i));
}
int* target = VECTOR_FIND(&v, int, 100, search_number);
if(target) {
printf("%d\n", *target);
}
vector_free(&v);
return 0;
}You can build this project using either CMake (recommended) or the provided Makefile.
- Generate the build directory:
make setup
- Compile the project:
make build-cmake
- Compile and run the compiled example:
make run-cmake
- Run tests:
make ctest
- Build and run test:
make check
- Compile the project:
make
- Run example:
make run
- Run tests:
make test - Clean the build files:
make clean