Thread: removing an element from an array

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    44

    removing an element from an array

    just wondering how i'd go about doing that? i haven't really started the program yet, i'm just trying to figure out all the things i need to do so i can break it up.

    the program involves an array of structures, to which i need to be able to or delete structures depending on user inputs. the function to add i don't think is too difficult, but i can't think of any way to delete an array element.

    any help would be appreciated

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    The simple way is
    array[pos] = array[pos+1]

    all the way through to the end of the array, from the point where you want to delete an element.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    kk that makes sense. now with structures, would i be doing essentially the same thing? for example, array[pos].x = array[pos+1].x?

    also, when i reach the last element, since no element exists after it, would it return that final element as a null element or as a bunch of random values stored in memory at that next location?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    You can assign whole structures (if you have an array of structures).

    You don't need to mess with assigning each member individually.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    alright that makes life easier in terms of structures.

    so i figured for the loop that would run the array[pos] = array[pos+1] i could stop at "length-1", but then my question becomes how would i deal with those last two elements, which now have the same value? i'd still need to delete the element at the tail end of the array...

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If order is not important, you could just
    eg.
    Code:
    temp = a[n-1];     // assume at least 1 element
    a[pos_to_del] = temp;  //overwrite with last element
    n--;                            // now array is less one element

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    unfortunately bayint, i need to put things in order...there are four values for each structure: two strings, an int and a float. for strings, i need to keep it alphabetical, and for the int and float it needs to be in numerical order. i don't see how your idea would affect that though? if i already have all the structures in the order i need them to be before i run the function to delete the element, it shouldn't matter anymore....right?

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Just try to implement salem method.
    Depending on what you are trying to do other data structures might be more suitable like linked list??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM