-
Notifications
You must be signed in to change notification settings - Fork 0
/
DinamicArray.c
84 lines (65 loc) · 1.83 KB
/
DinamicArray.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "DinamicArray.h"
void varray_init(varray **array) {
*array = (varray*) malloc (sizeof(varray));
(*array)->memory = NULL;
(*array)->allocated = 0;
(*array)->used = 0;
(*array)->index = -1;
}
void varray_push(varray *array, void *data) {
size_t toallocate;
size_t size = sizeof(void*);
if ((array->allocated - array->used) < size) {
toallocate = array->allocated == 0 ? size : (array->allocated * 2);
array->memory = realloc(array->memory, toallocate);
array->allocated = toallocate;
}
//printf("Dato %i \n", *(int*)(data));
array->memory[++array->index] = data;
array->used = array->used + size;
}
int varray_length(varray *array) {
return array->index + 1;
}
void varray_clear(varray *array){
int i;
for(i = 0; i < varray_length(array); i++)
{
array->memory[i] = NULL;
}
array->used = 0;
array->index = -1;
}
void varray_free(varray *array){
free(array->memory);
free(array);
}
void* varray_get(varray *array, int index){
if (index < 0 || index > array->index)
return NULL;
return array->memory[index];
}
void varray_insert(varray *array, int index, void *data){
if (index < 0 || index > array->index)
return;
array->memory[index] = data;
}
/*
int main(){
varray *array;
varray_init(&array);
int numero = 39;
int numero1 = 45;
int numero2 = 77;
void *ptrVoid = №
varray_push(array, ptrVoid); //Insercion de elementos
ptrVoid = &numero1;
varray_push(array, ptrVoid);
ptrVoid = &numero2;
varray_push(array, ptrVoid);
int pos = 3;
int largo = (int) varray_length(array);
int dato = *(int*)(varray_get(array, pos)); //Hay que hacer casting a un puntero del tipo necesario y ver el contenido del puntero
printf("Largo: %i y el valor de la posicion %i es: %i", largo, pos, dato);
return 0;
}*/