addition: Added basic header and structure
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
#include "arraylist.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
ArrayListResult arraylist_init(size_t capacity, size_t elem_size) {
|
||||
if (capacity == 0) {
|
||||
return (ArrayListResult) {
|
||||
.is_valid = false,
|
||||
.err = ARRLIST_INVALID_CAPACITY};
|
||||
}
|
||||
|
||||
if (elem_size == 0) {
|
||||
return (ArrayListResult) {
|
||||
.is_valid = false,
|
||||
.err = ARRLIST_INVALID_ELEM_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
ArrayList arr = {
|
||||
.data = malloc(capacity * elem_size),
|
||||
.capacity = capacity,
|
||||
.elem_size = elem_size,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
if (arr.data == NULL) {
|
||||
return (ArrayListResult) {
|
||||
.is_valid = false,
|
||||
.err = ARRLIST_BAD_ALLOC,
|
||||
};
|
||||
}
|
||||
|
||||
return (ArrayListResult) {.is_valid = true, .array = arr};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user