wrapping my head around this double pointer thing:

This commit is contained in:
Emanuel Rodriguez 2023-07-22 02:03:06 -07:00
parent b63d729b87
commit 52dd7164c5
1 changed files with 34 additions and 0 deletions

View File

@ -10,6 +10,12 @@ typedef struct i32_ArrayList {
i32_ArrayList* new_arraylist(int cap) {
i32_ArrayList* arr = malloc(sizeof(i32_ArrayList) + cap * sizeof(int));
if (arr == NULL) {
printf("ERROR: there was an error attemping to allocate memory for i32_ArrayList\n");
exit(1);
}
arr->capacity = cap;
arr->index = 0;
@ -26,6 +32,29 @@ void array_append(i32_ArrayList* s, int v) {
}
}
void resize_arraylist(i32_ArrayList** arr) {
int new_size = (*arr)->capacity * 2;
i32_ArrayList* new_arr = realloc((*arr), (sizeof(int) * new_size) + sizeof(i32_ArrayList));
if (new_arr == NULL) {
fprintf(stderr, "ERROR: unable to resize array\n");
exit(1);
}
(*arr) = new_arr;
(*arr)->capacity = new_size;
}
void array_append2(i32_ArrayList* arr, int v) {
if (arr->index == arr->capacity) {
// lets just double the capacity
resize_arraylist(&arr);
printf("size of arr: %d\n", arr->capacity);
}
array_append(arr, v);
}
// create an array list and fill in with values from array
i32_ArrayList* new_arraylist_from_array(int cap, int* arr) {
i32_ArrayList* out = new_arraylist(cap);
@ -125,6 +154,11 @@ int main() {
array_insert_at(a, 3, 123);
print_array_list(a);
// lets implement v2 versions of these function that will grow
// the array when required
array_append2(a, 5656);
print_array_list(a);
// array_append(a, 14);
// print_array_list(a);
// pop_from_array(a);