OneBite.Dev - Coding blog in a bite size

remove item from array in C

Code snippet on how to remove item from array in C

  void removeItem(int index, int arr[], int size) 
  { 
    int i; 
    for (i=index; i<size-1; i++) 
        arr[i] = arr[i+1]; 
  } 

This code is used to remove an item from an array in C programming. It takes three parameters: the index of the item to remove, the array, and the size of the array. It then uses a for loop to loop through the array starting with the index of the item to remove. Within the loop, it shifts all of the items with an index higher than the item being removed down one spot in the array, effectively overriding the item being removed and pushing all of the other items up. Once the loop is finished, the item is effectively removed from the array.

c