vendredi 27 mars 2015

Undo in C, copy struct



I'm new to C and I'm trying to simulate an Undo functionality for a problem. I'm using generic vectors defined like this:



typedef void* Element;

typedef struct {
Element* elems;
int size;
int capacity;
} Vector;


For this, I created a function called "Copy" that should return me a copy of the vector I'm passing:



Vector* copyVector(Vector *v) {
Vector* rez;
rez = createVector();
int i;
for (i = 0; i < getSize(v); i++) {
Element el = getElem(v, i);
add(rez, el);
}
return rez;
}


It works when I call it everytime to save the "before" vector... like when I try to apply an add or remove on my current vector, I call this copy function first on another vector called undoVec like this:



undoVec = copyVector(v);


I checked and it works but when I call my undo function... which should do the reverse of the code before:



v = copyVector(undoVec);


It's not working anymore. Doesn't do anything. It wont modify my vector v... which is really just a pointer I think



void undoVector(Vector *v, Vector *undoVec)


What am I doing wrong? why wont this functionality work? I can paste more code or give more info if required, thanks.



void add(Vector *v, Element elem) {
if (v->size == v->capacity) {
isFull(v);
}
v->elems[v->size] = elem;
v->size++;
}

Element getElem(Vector *v, int pos) {
return v->elems[pos];
}



Aucun commentaire:

Enregistrer un commentaire