Insertion and Deleting a value into an Array using C programming.

Introduction :

          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 :

         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 :

a[i]=x;

Deletion :

        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 :

a[1]=a[2];

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.

a[2]=NULL;

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.

a[1]=NULL;

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.

Array Index:

           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.

Array size:

            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.

References:

https://www.geeksforgeeks.org/c-program-to-insert-an-element-in-an-array/
https://www.programmingsimplified.com/c/source-code/c-program-insert-element-in-array
https://www.programmingsimplified.com/c/source-code/c-program-insert-element-in-array

Default image
Karan
I'm Karan Dharmalingam, Co-founder of Criss Cross Tamizh. My responsibilities in Criss Cross Tamizh include Head Content Writer and Editor, Video Producer, and Editor. I have always believed that my best work is in the content creation field. The journey of CCT was almost like a bar joke. While talking to my bestie Arun Kumar, he jokingly said how cool it would be if we can have our own websites and YouTube channels. And I also felt the same and right at that moment we started a YouTube channel and it's almost 2 years and we have come a very long way both in business and as humans as well. Thanks for reading nanba/nanbi !!
Articles: 4

Leave a Reply