Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C program to delete an array element using Pointers
In C, deleting an element from an array involves shifting all subsequent elements one position to the left. This operation can be efficiently implemented using pointers to manipulate array elements directly through memory addresses.
Syntax
void deleteElement(int *array, int size, int position); // array: pointer to the array // size: current size of the array // position: 1-based position of element to delete
Algorithm
The algorithm to delete an element from an array using pointers −
- Declare and read the array size
- Allocate memory dynamically using malloc()
- Input array elements
- Read the position of element to be deleted
- Validate the position (must be within array bounds)
- Shift all elements after the deleted position one position to the left
- Display the updated array
Example
Following is the C program to delete an element from an array using pointers −
#include <stdio.h>
#include <stdlib.h>
void deleteElement(int n, int *a, int pos);
int main() {
int *a, n, i, pos;
printf("Enter the size of array: ");
scanf("%d", &n);
a = (int*)malloc(sizeof(int) * n);
if (a == NULL) {
printf("Memory allocation failed<br>");
return 1;
}
printf("Enter the elements:<br>");
for (i = 0; i < n; i++) {
scanf("%d", (a + i));
}
printf("Original array: ");
for (i = 0; i < n; i++) {
printf("%d ", *(a + i));
}
printf("<br>");
printf("Enter the position of element to be deleted (1 to %d): ", n);
scanf("%d", &pos);
deleteElement(n, a, pos);
free(a);
return 0;
}
void deleteElement(int n, int *a, int pos) {
int i;
if (pos < 1 || pos > n) {
printf("Invalid Input<br>");
return;
}
// Shift elements to the left
for (i = pos - 1; i < n - 1; i++) {
*(a + i) = *(a + i + 1);
}
printf("After deletion the array elements are:<br>");
for (i = 0; i < n - 1; i++) {
printf("%d ", *(a + i));
}
printf("<br>");
}
Output
Enter the size of array: 5 Enter the elements: 12 34 56 67 78 Original array: 12 34 56 67 78 Enter the position of element to be deleted (1 to 5): 3 After deletion the array elements are: 12 34 67 78
How It Works
- Memory Allocation: Dynamic memory is allocated using malloc() to create an array of specified size
- Pointer Arithmetic: Elements are accessed using *(a + i) notation instead of array subscript
- Element Shifting: When deleting at position pos, all elements from position pos+1 onwards are moved one position left
- Validation: Position is checked to ensure it's within valid range (1 to n)
Key Points
- The array size effectively reduces by 1 after deletion
- Position validation prevents accessing invalid memory locations
- Always free() dynamically allocated memory to prevent memory leaks
- Time complexity is O(n) due to the shifting operation
Conclusion
Array deletion using pointers demonstrates efficient memory manipulation in C. The pointer-based approach provides direct access to memory locations while maintaining the logical structure of the array after element removal.
Advertisements
