C program to delete an element from an array
This tutorial will show you how to delete an element from an array in C.
Problem description #
We have an array of integers and we want to delete an element from it.
Examples #
Problem Solution #
- Create an array of integers.
- Ask the user to enter the index of the element to be deleted.
- Delete the element from the array.
- Print the array.
Delete element by index #
In this approach, we will use a loop to iterate through the array and delete the element from the array.
Approach #
- Create an array of integers.
- Ask the user to enter the index of the element to be deleted.
- Loop through the array and delete the element from the array.
- Print the array.
Program/Source code #
Explanation #
The program will ask the user to enter the size of the array and then the elements of the array. The program will then ask the user to enter the index of the element to be deleted. The program will then loop through the array and delete the element from the array. The program will then print the array.
Space Complexity #
The space complexity of this program is O(1).
Time Complexity #
The time complexity of this program is O(n).
Output #
Delete element by value #
In this approach, we will use a loop to iterate through the array and delete the element from the array.
Approach #
- Create an array of integers.
- Ask the user to enter the value of the element to be deleted.
- Loop through the array and delete the element from the array.
- Print the array.
Program/Source code #
Explanation #
The program will ask the user to enter the size of the array and then the elements of the array. The program will then ask the user to enter the value of the element to be deleted. The program will then loop through the array and delete the element from the array. The program will then print the array.
Space Complexity #
The space complexity of this program is O(1).
Time Complexity #
The time complexity of this program is O(n).
Output #
Advanced approach #
In this approach we’ll use separate functions to delete elements by index or by value and give back the array after deleting the element.
Approach #
- Create an array of integers.
- Give the user the option to delete an element by index or by value.
- Delete the element from the array.
- Print the array.
Program/Source code #
Methods used #
- delete_element_by_index() — This function will delete the element from the array by index.
- delete_element_by_value() — This function will delete the element from the array by value.
- print_array() — This function will print the array.
Explanation #
The program will ask the user to enter the size of the array and then the elements of the array. The program will then ask the user to enter the index of the element to be deleted. The program will provide a choice between deleting element by value or by index. The program will then call the function delete_element_by_index() to delete the element from the array. The program will then call the function print_array() to print the array. The program will then ask the user to enter the value of the element to be deleted. The program will then call the function delete_element_by_value() to delete the element from the array. The program will then call the function print_array() to print the array.
How do I clear an array in C?
What would I use to clear the contents of x ? I’m not able to re-initialise it, use strcpy(x, ‘/0’) or free() .
![]()
4 Answers 4
You cannot assign anything to an array, which your variable x is. So therefore anything that starts with x = is wrong. Secondly ‘hello’ is not a string, it is a multicharacter literal which is of type int , so this doesn’t make sense either. A string literal is enclosed by » while character (or multicharacter) literals are enclosed by ‘ .
So if you want to fill your buffer x with the string «hello» you use strncpy or even better strlcpy if available:
The strlcpy function is better because it always terminates the string with a nul character.
If you want to clear it you could do what the other answers suggested. I’d suggest using strncpy or strlcpy with an empty string as @codaddict suggested. That is the code that says most obviously «hey, I want to clear that string». If you want to remove the whole contents of the string from memory (for example if it contained a password or something like this) use memset as @Ken and @Tom suggested.
Also note that you never ever use functions like strcpy or strcat that don’t accept the size of the output buffer as a parameter. These are really not secure and cause nasty bugs and security vulnerabilities. Don’t even use them if you know that nothing can go wrong, just make a habit of using the secure functions.
Как очистить элементы массива?
Скажите, а можно как-то очистить массив? Допустим задан массив типа int mas[100]. После некоторых вычислений, он заполнился следующими значениями, к примеру:
mas[0] = 20
mas[1] = 23
mas[2] = 35
.
mas[70] = 55
Но мне нужно его очистить, чтобы записать новые значения. Подскажите, как это сделать?
Как очистить первый элемент массива
Добрый день. Как очистить первый элемент массива, чтобы значение первого можно было добавить.
Как очистить TextBox для ввода массива?
Подскажите пожалуйста метод, чтобы после нажатия кнопки стиралось все, что было написано в TextBox.
C Language Arrays Clearing array contents (zeroing)
Sometimes it’s necessary to set an array to zero, after the initialization has been done.
An common short cut to the above loop is to use memset() from <string.h> . Passing array as shown below makes it decay to a pointer to its 1st element.
As in this example array is an array and not just a pointer to an array’s 1st element (see Array length on why this is important) a third option to 0-out the array is possible: