-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray_obj.c
64 lines (57 loc) · 1.58 KB
/
array_obj.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "objects.h"
#include "array_obj.h"
/*
Array strcuture for objects, but it it only workes for 50 obj,
becuase reallocation is not working
by Goran Topic
*/
Obj_arr* make_obj_ar(int size){
// make and array of Objects
Obj_arr* arr = malloc(sizeof(Obj_arr));
arr->capacity = size;
arr->objs = calloc(arr->capacity, sizeof(Object));
arr->count = 0;
return arr;
}
void remove_obj_at(Obj_arr* array, int index){
// removes an obj from the array of objs
if(index >= array->count){
// index greater
printf("ERROR: Could not remove index higher than count\n");
return;
}else if ( index == array->count - 1){
//remove last element
free_obj( &(array->objs[array->count -1]));
array->count--;
return;
}else{
Object last = array->objs[array->count - 1];
array->objs[index] = last;
free_obj( &(array->objs[array->count - 1]) );
array->count--;
}
}
void append_obj(Obj_arr* array, Object* obj){
// appends a obj to the array of Objs
if(array->count >= array->capacity)
//it it reaches capacity then get more
printf("Could not append obj max cap of: %d reached", array->capacity);
array->objs[array->count] = *obj;
array->count++;
}
void print_array(Obj_arr* array){
//print the array
for(int i = 0; i < array->count; i++){
//print model object
printf(" %s \n", array->objs[i].title);
for(int line = 0; line < array->objs[i].model_c; line++){
printf("%s\n", array->objs[i].model[line]);
}
printf("\n");
}
printf("count: %d\n", array->count);
}