Hello, I made a vector struct that I tested and thought was working like I wanted but now it does something that I don't want.
my vector.h
Code:
typedef struct vector {
void **data;
int capacite;
int nbData;
} vector;
void vector_init(vector *);
int vector_total(vector *);
void vector_resize(vector *, int);
void vector_add(vector *, void *);
void vector_set(vector *, int, void *);
void *vector_get(vector *, int);
void vector_delete(vector *, int);
void vector_empty(vector *);
void vector_print(vector *);
void vector_free(vector *);
some parts of my vector.c (don't to flood with codes)
Code:
void vector_init(vector *v){
v->capacite = capacite_ini;
v->nbData = 0;
v->data = calloc(v->capacite,sizeof(void *));
//v->data = malloc(sizeof(void *) * v->capacite);
}
void vector_add(vector *v, void *item){
if (v->capacite == v->nbData)
vector_resize(v, v->capacite * 2);
v->data[v->nbData++] = item;
}
In my main file, I'm reading words from a file and adds them to my vector but I'm adding a pointer so all the ''words'' in my vector are the same and change.
my main:
Code:
char mot[30];
char c;
int j=0;
while((c = fgetc(fIN)) != EOF){
if(c == '\n')
continue;
if(j != 0 && c == ' '){
mot[j]='\0';
vector_add(&vector_dut,mot);
j=0;
mot[0] = '\0';
vector_print(&vector_dut);
}
mot[j]=c;
j++;
}
...
I know in other language, it would have been possible to do vector_dut(new String temp)...
thanks in advance!