Tired of writing the same old allocation and reallocation code whenever you want to resize arrays in C? Sick of having separate variables to keep track of your array's length? Or initalizing its elements to zero? This library offers arrays with a few extra features bolted on.
- Easy to Use (library only has four functions)
- Generic (can hold any kind of data)
- Intuitive (behaves like a normal array with indexing e.g. arr[i])
- Lightweight (less than 50 lines of source code)
- Performant (memory aligned elements)
- Portable (only uses the C standard library)
stddef.hstdlib.hstring.h
- Resizable (can grow in length)
Compute factorial using a dynamic array:
int factorial(int number) {
int *n = arr_Create(0, sizeof(int));
for(int i = 2; i <= number; i++) {
n = arr_Append(n, &i, 1);
}
number = 1;
for(int i = 0; i < arr_Length(n); i++) {
number *= n[i];
}
arr_Destroy(n);
return number;
}
arr$ make factorial
gcc -fPIC -shared -I ./include ./src/arr.c -o ./arr.so
gcc -I ./include ./arr.so ./examples/factorial.c -o ./factorial.bin
./factorial.bin
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
rm ./factorial.bin
Returns a allocated array with the specified length and element size. Returns null if the allocation failed.
lengthnumber of element in the array.sizesize of each element.
Deallocates array.
arrayarray returned by arr_Create. Non null value.
Returns the number of elments in the array.
arrayarray returned by arr_Create. Non null value
Returns the reallocated array with source elements appended. Returns null if the reallocation failed.
arrayarray returned by arr_Create. Non null value.sourcesource array to be copied. Non null value.lengthlength of the source array.