I want to remove elements from struct array. I have tried many different versions of my remove()function, but no of them work. This last deletes the element that user spcified and copies onther element to its place.
in main() i have n=remove(a,n)

int remove(Vegies a[], int n){

  int i,input,j;
            while(1){                             
                printf("Item number to delete: ");
                scanf("%d", &input);

                for(i = 0; i < n; i++){
                    if(input == a[i].itemN){
                        for (j = 0; j <  n; j++){           
                        a[j] = a[j + 1];
                        (n--);
                        i--;
                        }            
                    }            
                    if(input==0){
                        return 0;
                    }
                }                                 
            }return 0;

}

I'm assuming ordering is important. If not you could just copy the last element to the nth (deleted) position and decrease the number of items by one.

If ordering IS important then why bother to loop over the entire array? All you need to do is loop from the nth element to the last-1 and copy down. For example, if you have an array of

0. orange
1. apple
2. banana
3. pear
4. mango
5. peach

and you want to delete pear you only have to loop over 3 and 4. Copy 4 to 3, and 5 to 4. If you have a very large array this potentially saves a lot of looping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.