Earlier we have seen simple way to insert an element into an array but now let’s see how to delete an element from an array. Before that I want to clear a myth, that we can delete/earse data from the memory but that’s not right. Anything in memory is non-delatable, it can either be overwritten/remove the address stored in the pointer to make operating system not knowing about it. Similarly when we say we’re deleting an element from an array, we’re not actually deleting it but just saying OS not to worry about that memory address and forget about it through a simple programming logic. If this myth is clear, we can now goto the program.
Array Deletion Program:
The instruction from the line 1 -6 are just comments. Comments are basically used to describe your program/instructions. These comments will be ignored by the compiler. Usually we use // to comment single line but if you want to comment multiple lines at the same time you can also use /* <Comments> */.
On the line 8, I just declared a standard input/output library. This header has all the printf and scanf API built-in. By the way don’t get confused with the term headers & libraries. Usually in C we call them headers but in java we call them libraries, just a terminology difference but the meaning is same.
On line 9, I have declared a macro “ARRAYSIZE” and assigned a value of 5. This I have declared just for better readability when you read the variable/code. These predefined macros will be compiled initially by the compiler.
Next inside main, I have taken input values from the user to an array. Once it’s done using an identifier “pos” I’ve requested user to enter the portion in where the element needs to be deleted from an array. After the position has been entered, I’ve checked it’s not greater than the ARRAYSIZE we’ve declared. If the position is valid, then we just overridened the value into that position the successor values and printed the values.
Here I have taken 5 input elements from user to push into an array
Next I have taken the position in which the element has to be deleted from array
Once the element is deleted, I have printed the final list/elements in an array.