-
Notifications
You must be signed in to change notification settings - Fork 1
/
array2.c
66 lines (46 loc) · 1.02 KB
/
array2.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
#include <stdio.h>
#include <stdlib.h>
#include "array2.h"
struct array *newArrayString() {
struct array *arr = (struct array *)malloc(sizeof(struct array *));
char** buff;
buff = (char **)malloc(sizeof(char *)*(20));
arr->vals = buff;
arr->size = 20;
int i;
for(i = 0; i < 20; i++)
{
arr->vals[i] = 0;
}
return arr;
}
char *getVal(struct array *a, int index) {
return(a->vals[index]);
}
void setVal(struct array *a, char *newVal, int index) {
if(a->size >= index) {
resize(a);
}
a->vals[index] = newVal;
}
void resize(struct array *a) {
char** buff;
buff = (char **)malloc(sizeof(char *)*((a->size) * 2));
int i;
for(i = 0; i < a->size; i++) {
buff[i] = a->vals[i];
}
char **temp = a->vals;
a->vals = buff;
a->size = (a->size) * 2;
int j;
for(j = (a->size)/2; j < (a->size); j++)
{
a->vals[j] = 0;
}
free(temp);
}
void freeArray(struct array *a) {
free(a->vals);
free(a);
}