I am trying to write 2 functions in C, one of which would add an element to the end of my array that i pass as an argument, and the other function which would delete an element from the array passed as an argument.
I am not really familiar with C, and not good into pointers arithmetic.
I have tried with something like this, but it seems not to be working properly:
void remove_element(type *elements, type element, int size){
int i;
int index = 0;
for(i=0; i<size; i+=1){
if(equals(elements[i], element)) index = i;
}
for(i=index; i<size-1; i+=1){
elements[i] = elements[i+1];
}
}
void add_element(type *elements, type element, int size){
elements = realloc(elements, size*sizeof(element));
elements[size-1] = element;
}
That is how I want to call the functions:
add_element(elements, new_element, size);
remove_element(elements, element_to_remove, size);
main, no input, no output of any kind.add_element()function allocates a new array, but it never returns it to the caller.realloc()every time you add an element seems like very poor design. You should use a structure that keeps track of the allocated size and the number of elements that are in use, and only reallocate when you need to go past the size.