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 search an array element using Pointers.
In this tutorial, we'll learn how to search for an element in an array using pointers in C. This approach demonstrates the power of pointer arithmetic and dynamic memory management for array operations.
Syntax
int search(int *array, int size, int element); // Returns: index if found, -1 if not found
Algorithm
Here's the step-by-step algorithm to search elements in an array using pointers −
- Step 1 − Declare and read the array size
- Step 2 − Input array elements
- Step 3 − Declare a pointer to traverse the array
- Step 4 − Input the element to be searched
- Step 5 − Use pointer arithmetic to traverse and compare elements
- Step 6 − Display the search result
Method 1: Using Pointer with Function
This method passes the array to a search function using a pointer parameter −
#include <stdio.h>
int search(int *arr, int size, int element) {
for (int i = 0; i < size; i++) {
if (*(arr + i) == element) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}
int main() {
int n, element, result;
printf("Enter the size of array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:<br>");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &element);
result = search(arr, n, element);
if (result != -1) {
printf("%d is present in the array at index %d<br>", element, result);
} else {
printf("%d is not present in the array<br>", element);
}
return 0;
}
Enter the size of array: 5 Enter the elements: 14 12 11 45 23 Enter the element to be searched: 11 11 is present in the array at index 2
Method 2: Using Direct Pointer Arithmetic
This approach uses pointer arithmetic directly in the main function −
#include <stdio.h>
int main() {
int n, element, found = 0, position = -1;
printf("Enter the size of array: ");
scanf("%d", &n);
int arr[n];
int *ptr = arr; // Pointer to the array
printf("Enter the elements:<br>");
for (int i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
printf("Enter the element to be searched: ");
scanf("%d", &element);
// Search using pointer arithmetic
for (int i = 0; i < n; i++) {
if (*(ptr + i) == element) {
found = 1;
position = i;
break;
}
}
if (found) {
printf("%d is found at position %d<br>", element, position);
} else {
printf("%d is not found in the array<br>", element);
}
return 0;
}
Enter the size of array: 4 Enter the elements: 10 20 30 40 Enter the element to be searched: 25 25 is not found in the array
Key Points
-
Pointer Arithmetic:
*(ptr + i)is equivalent toptr[i] - Array Name: The array name itself acts as a pointer to the first element
- Return Values: Using return values makes functions more reusable
- Time Complexity: Linear search has O(n) time complexity
Conclusion
Searching arrays using pointers demonstrates the relationship between arrays and pointers in C. This approach provides flexibility in memory management and efficient array traversal using pointer arithmetic.
Advertisements
