As we discussed in an earlier post about Arrays in C Programming. In this article, we will discuss how to do insertion, deletion in arrays. As you might know, the arrays are nothing but a continuous collection of variables of the same type. So we have to do save the values inside the arrays in C programming.
Insertion in Arrays is the process of inserting values into the array variables or indices. The values can be inserted into the arrays by using their index or otherwise known as the array index. The syntax to insert a value of x into an array an at index i is :
Deletion in Arrays is the process of removing or deleting a value from the array. The deletion operation in an array in c programming can be performed by moving the next element to the position to the position where you want to delete. For example, let us consider an array of name a with 3 elements which would look like the following array :
Here to remove value 2 from position 1 we need to move the value 3 to position 1 from its current position 2. It can be done by doing the following :
This is how it will look :
Now, as you might notice by now that our deletion in arrays ie the moving the element next to it causes duplication. To avoid duplication we need to assign a value of null to the duplicated value. This can be done by the following c programming code.
It will look like this:
This method creates no duplication but this process of deletion in arrays caused an element to move forward from its original position. To avoid this we can do this with another method. That is directly assigning the null value to the position we want to dele Now, as you might notice by now that our deletion in arrays ie the moving the element next to it causes duplication. To avoid duplication we need to assign a value of null to the duplicated value. This can be done by the following c programming code.
his is not advisable as it will create an empty position in the array.
The important parameters while doing insertion and deletion in arrays in c programming are :
- Array variable,
- Array index,
- Value to be inserted,
- Array size.
- Array Variable :
The array variable or the array identifier is the name of the array as if you mistype the array it might show an error or will wrongly delete an element in another array.
The array index or the position of the value in the array you want to delete is important. As the programming code will only delete the value in the index that you entered.
Value to be inserted:
You need to be sure about the value that you want to delete and its position. As with mistyping it will also trigger another element to delete.
If you’re inserting a value to an array, make sure that the array’s initial size is not exceeded. Because even if you enter a single value more than the actual array size, it will cause an array overflow error.