I've created a function that allows an element to be deleted from my malloc(ed) array (a list of struct triangle coordinates). That works fine. But when I tried to create one that adds an element (after position i) it just won't work...
For example, if I had an array of [0] [1] [2] [3] [4] [5] and wanted to add a new value after [3].
Here is my code: (this is a snippet of inside my while loop)
totaltri++;
addtriangle(trilist, totaltri, i);
i++;
secondtriangle(p, &t1, &t2, &t3, v, i);
i++;
My code deletetriangle(....):
for (int c = i; c < totaltri; c++)
{
trilist[c] = trilist[c+1];
}
tmp = (triangle *) realloc(trilist, (totaltri-1)*sizeof(triangle));
trilist = tmp;
And my code addtriangle(....): Which doesn't seem to work..!
tmp = (triangle *) realloc(trilist, (totaltri+1)*sizeof(triangle));
for (int c = totaltri; c > i; c--)
{
trilist[c-1] = trilist[c];
}
trilist = tmp;
I then proceed to assign values to the second triangle that has just been added in this code:
int p1, p2, p3;
p1 = whereisthepoint(p, *t1, v);
p2 = whereisthepoint(p, *t2, v);
p3 = whereisthepoint(p, *t3, v);
triptr=&(trilist[i]);
triptr->corner=(point *) malloc(3*sizeof(point));
pt1ptr=&(triptr->corner[0]);
pt2ptr=&(triptr->corner[1]);
pt3ptr=&(triptr->corner[2]);
pt3ptr->x=t3->x; etc etc