How to Delete an Element from an Array of Structs in C? Last Updated : 14 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C, an array of structs refers to the array that stores the structure variables as its elements. In this article, we will learn how to delete an element from an array of structures in C. For Example, Input: struct Person persons[3] = { { "Person1", 25 }, { "Person2", 30 }, { "Person3", 22 }, }; TargetToDelete= "Person2" Output: Array of Persons after deletion: Name: Person1, Age: 25 Name: Person3, Age: 22Remove an Element from an Array of Structure in CWe cannot directly remove the element for an array in C so to remove an element from an array of structures, we need to overwrite the target element that we want to remove by shifting all elements after the target element one position toward the beginning of the array. C Program to Delete an Element from an Array of StructureThe below example demonstrates how we can remove the element from an array of structures in C. C // C program to delete an element from an array of structure #include <stdio.h> #include <string.h> struct Person { char name[50]; int age; }; #define MAX_PERSONS 10 // function to print the content of array of structure void displayPersons(struct Person persons[], int count) { printf("Array of Persons after deletion:\n"); for (int i = 0; i < count; ++i) { printf("Name: %s, Age: %d\n", persons[i].name, persons[i].age); } printf("\n"); } int main() { // intializing array of structure struct Person persons[MAX_PERSONS] = { { "Person1", 25 }, { "Person2", 30 }, { "Person3", 22 }, }; int MAX_PERSONS_CURRENT = 3; // Initial size of the array // Identifying the Element to Delete const char* nameToDelete = "Person2"; int indexToDelete = -1; // Finding the index of the person to delete for (int i = 0; i < MAX_PERSONS_CURRENT; ++i) { if (strcmp(persons[i].name, nameToDelete) == 0) { indexToDelete = i; break; } } // Checking if the person was found if (indexToDelete == -1) { printf("Person with name %s not found.\n", nameToDelete); } else { // Deleting the Element for (int i = indexToDelete; i < MAX_PERSONS_CURRENT - 1; ++i) { persons[i] = persons[i + 1]; } // Adjusting the Array Size MAX_PERSONS_CURRENT--; // Displaying the Updated Array displayPersons(persons, MAX_PERSONS_CURRENT); } return 0; } OutputArray of Persons after deletion: Name: Person1, Age: 25 Name: Person3, Age: 22Time Complexity: O(N), where N is the size of the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C Program to Delete an Element in an Array H heysaiyad Follow Improve Article Tags : C Programs C Language C-Arrays C-Structure & Union C Examples +1 More Similar Reads How to Add an Element to an Array of Structs in C? In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra 3 min read C Program to Delete an Element in an Array In this article, we will learn to how delete an element from an array in C. C arrays are static in size, it means that we cannot remove the element from the array memory. But we can logically delete the element by overwriting it and updating the size variable.Delete the Given Element from an ArrayTo 3 min read How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 2 min read How to Read a Struct from a Binary File in C? The structure in C allows the users to create user-defined data types that can be used to group data items of different types into a single unit. We can save this structure in a file using C file handling. In this article, we will learn how we can read a struct from a binary file in C. Read a Struct 2 min read How to Create a Dynamic Array of Structs? In C, we have dynamic arrays in which we can allocate an array of elements whose size is determined during runtime. In this article, we will learn how to create a dynamic array of structures in C. Create a Dynamic Array of Structs in CA dynamic array of structs in C combines dynamic arrays and struc 2 min read How to Dynamically Create Array of Structs in C? In C, an array is a data structure that stores the collection of elements of similar types. Structs in C allow the users to create user-defined data types that can contain different types of data items. In this article, we will learn how we can create an array of structs dynamically in C. Dynamicall 2 min read Like