Delete an Element from Array

This example will show you how to delete an element from an Array using C program.

The below delete function takes three arguments

void delete(int *a, int n, int i)

*a – pointer to an array

n – size of the array

i – at what position of the array the element will be deleted

Example

#include<stdio.h>

void delete(int *a, int n, int i){
	int j,l;
	if(n<=i){
		printf("Deletion index must be less than size of the array");
		return;
	}
	for(j=i+1;j<n;j++){
		a[j-1]=a[j];
	}
	for(l=0;l<n-1;l++){
		printf("a[%d]=%d ",l,a[l]);
	}
}

main(){
	int i;
	int size=5;
	int a[5];
	for(i=0;i<size;i++){
		a[i]=i+100;
	}
	delete(a,size,3);
	return;
}

Output

a[0]=100 a[1]=101 a[2]=102 a[3]=104

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *