-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.h
More file actions
36 lines (31 loc) · 772 Bytes
/
array.h
File metadata and controls
36 lines (31 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
float *data;
size_t size;
size_t capacity;
} Array;
Array newArray(size_t initialCapacity) {
Array arr;
arr.data = (float*)malloc(initialCapacity * sizeof(float));
arr.size = 0;
arr.capacity = initialCapacity;
return arr;
}
void resizeArray(Array *arr, size_t newCapacity) {
arr->data = (float*)realloc(arr->data, newCapacity * sizeof(float));
arr->capacity = newCapacity;
}
void push(Array *arr, float element) {
if (arr->size == arr->capacity) {
resizeArray(arr, arr->capacity * 2);
}
arr->data[arr->size] = element;
arr->size++;
}
void freeArray(Array *arr) {
free(arr->data);
arr->size = 0;
arr->capacity = 0;
}