https://github.com/franko/libarray
Very simple C library for generic dynamic arrays
https://github.com/franko/libarray
c generic-dynamic-arrays
Last synced: 10 months ago
JSON representation
Very simple C library for generic dynamic arrays
- Host: GitHub
- URL: https://github.com/franko/libarray
- Owner: franko
- Created: 2018-12-09T21:52:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-28T22:04:17.000Z (over 7 years ago)
- Last Synced: 2025-01-20T14:40:11.058Z (over 1 year ago)
- Topics: c, generic-dynamic-arrays
- Language: C
- Size: 28.3 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Libarray
Very simple C library for generic dynamic arrays. It works in a similar way to C++ templates but using macros.
It works only for plain old data that do not need memory allocation or a destructor.
Please note that the library is just in early development and many functions are missing.
## Motivation
In C is a common pattern to use arrays that dinamically grows as needed when more elements are added.
The library avoid writing always the same code over and over again and it provides a type-safe implementation.
Here an example:
```c
#include
#include "array.h"
/* Declare an array of int. Using the inline we don't need to provide an implementation
in a separate file. */
declare_array_inline(int);
int main() {
/* The type array(int) identifies the array of int. */
array(int) myarray = array_init();
/* All the method will use the sintax: array_(int)(...). For example array_push(int) is
used to append elements into the array. */
for (int i = 0; i < 100; i++) {
array_push(int)(myarray, i * i);
}
/* Elements can be read by using either the 'element' macro or with the 'array_get(int)' function. */
for (int i = 0; i < myarray->length; i++) {
printf("%i\n", element(myarray, i));
}
array_free(int)(myarray);
return 0;
}
```
## How to use
Just add 'array.h' and 'array.c' into your project, that's all.
Libarray is currently provided with a Meson build file and a few simple tests but these are not needed to use the library.