0

I have a structure:

typedef struct score_entry
{
    char name[21];
    int score;
} score_entry;

and an array: score_entry readin[1000];

I want to write a function that consumes a score_entry array and a name(String) and remove all structures in the array with that name and return the new array. Is this possible? if so how could it be done ?

2
  • Which part are you having problems with? Commented Mar 27, 2012 at 19:06
  • If you're using static array, you can't remove items from it. Use linked lists instead Commented Mar 27, 2012 at 19:08

4 Answers 4

2

Well you can't "remove" elements from an array in C. But you can count the elements that does not match name, create a new array on the heap, and copy all elements of interest. The basic code could look like this, however, you should make it safe, don't use the same identifier for the struct tag and typename, and return the size of the new array.

  score_entry *sortout(score_entry *array, char* string) {
     score_entry *newarray;
     int i, n=0;

     /* measure the size of the filtered array copy */
     for(i=0; i<1000; i++) {
        if (strcmp(array[i].name, string) n++;
     }

     /* allocate space for the filtered copy */
     newarray = (score_entry*)calloc(n, sizeof(score_entry));

     /* filter and copy the data */
     for(i=0, n=0 ; i<1000; i++) {
        if (strcmp(array[i].name, string))
           newarray[n++] = array[i];
     }
  return newarray;

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

7 Comments

but i cant like edit the array ? .... say i want to like readin = sortout(readin, "somestring") ... will readin be the new array or no?
Note that this approach is expensive, memory and CPU-wise. The linked-list approached listed above are probably better, though this is truer to the original question.
@Beginnernato: no, you can't. However, if the first array on the heap to, you could free it after this procedure. But then, it would be probably better to sort all elements to array's begin and do a realloc. @Randall Cook: You are right. I tried to answer the question, however, I would never recommend this approach.
I'm trying to find a way to this without using heap storage, so that's why i was wondering it its possible
Is there any reason not to use the heap?
|
0

I'd probably just use a linked list and then delete elements in question. Otherwise you'd have to iterate over your array and skip/move entries not matching your search string/name.

Comments

0

Array elements cannot be removed once created. Instead of

score_entry readin[1000];

Consider creating a linked list. First add a new element to the structure

typedef struct score_entry
{
    char name[21];
    int score;
    struct score_entry *next;
} 

And then look into any example of creating singly linked lists and then proceed to your new function implementation wherein you can easily delete nodes which match a criteria

Comments

0

How do you keep track of the number of elements in the array? Although you can't delete elements; you can squish them out, then decrement the count, for example:

void 
delete(score arr[], int *nelem, score target){
    int hi, lo, count;
    for(hi=lo=0; hi<*nelem; hi++){
        arr[lo] = arr[hi];
        if(!same(target, arr[lo]))
            lo++;
    }
    *nelem = lo;
}

2 Comments

Think of two guys; they start at the bottom of the array, but the faster guy runs past all the elements you want to delete; the then throws all the elements you want to the slower guy, who puts them down in order; When you're done, you have a list of good elements followed by garbage. Then, *nelem = lo "returns" the number of elements in the "new" list.
Nope, all the good elements are next to each other at the beginning of the list; it returns the number of good elements.

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.