0

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);
9
  • Can you please elaborate on how it's not working properly? You might also want to show some code showing how you use these functions, please read e.g. sscce.org Commented Nov 2, 2013 at 22:17
  • Your code doesn't look like it actually puts anything into the array. Commented Nov 2, 2013 at 22:17
  • Describe "it seems not to be working properly". What input did you give your program? What results did you expect? what results did you actually get? I only see two functions in your code. No main, no input, no output of any kind. Commented Nov 2, 2013 at 22:18
  • Your add_element() function allocates a new array, but it never returns it to the caller. Commented Nov 2, 2013 at 22:19
  • 1
    Calling 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. Commented Nov 2, 2013 at 22:26

1 Answer 1

1

Theses two lines may cause undefined behavior:

cvorovi = realloc(elements, size*sizeof(element));
elements[size-1] = cv;

The realloc function is not guaranteed to return a pointer to the same area you want to reallocate, meaning that elements may not point to the allocated area after the call. You must use the returned pointer.

This will most likely cause problems with the code calling this function, as that code will not know anything about the change of the pointers. Either pass elements by reference (i.e. a pointer to pointer) or return the new pointer.

Also, after seeing your edit, you do remember to pass in a larger size than the current size when you call add_element? Otherwise it will just reallocate the same size over and over again, and overwriting the last inserted element each call. You may want to consider making a structure keeping track of these, and and special set of functions that takes this structure as argument and handles all the things (like keeping track of the size) internally.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually this line looks like this: elements = realloc(elements, size*sizeof(element)); The original line was a typo.
@Whizzil But the last part of the answer is still valid. How will the calling code know about the possible change?
I would appreciate a solution, an actual code of how it would work what i want, it would be much clearer that way. @edit: yes i do remember that :)
@Whizzil Just return the new pointer from add_element, and assign that in the caller. Like elements = add_element(elements, new_element, size + 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.