Write a C program to input elements in array and search an element in array using pointers. How to search an element in array using pointers in C programming. Logic to search an element in array using pointers in C program.
Example
Input
Input array elements: 10 20 30 40 50 60 70 80 90 100 Input element to search: 25
Output
25 does not exists in array.
Required knowledge
Basic C programming, Array, Functions, Pointer, Pointer Arithmetic, Pointer and Arrays
In my previous posts related to array, I have explained how easily we can search an element in array without using pointer. Here in this post we will see how to search an element in array using pointer.
Logic to search an element in array using pointers
Below is the step by step descriptive logic to search an element in array using pointer.
- Input size and elements in array. Store them in some variable say
size
andarray
. - Input element to search from user, store it in another variable say
toSearch
. - Initialize a pointer to first and last element of array. Say
arr = array
andarrEnd = (array + size - 1)
. - Initialize a variable
index = 0
that will keep track of searched element index. - For each array element increment
index
andarr
by 1 tillarr <= arrEnd
and*arr != toSearch
. - Finally if
(arr <= arrEnd)
, thentoSearch
element exists in array at positionindex + 1
otherwise not.
Program to search an element in array using pointers
/**
* C program to search an element in array using pointers
*/
#include <stdio.h>
#define MAX_SIZE 100
/* Function declaration */
void inputArray(int * arr, int size);
int search(int * arr, int size, int toSearch);
int main()
{
int array[MAX_SIZE];
int size, toSearch, searchIndex;
/*
* Input size and elements in array
*/
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
inputArray(array, size);
// Input element to search from user
printf("Enter element to search: ");
scanf("%d", &toSearch);
// Call search funtion to search element in array
searchIndex = search(array, size, toSearch);
// Print search results
if(searchIndex == -1)
printf("%d does not exists in array.", toSearch);
else
printf("%d is found at %d position.", toSearch, searchIndex + 1);
return 0;
}
/**
* Function to input elements in array.
*
* @arr Pointer to integer array
* @size Size of the array
*/
void inputArray(int * arr, int size)
{
// Pointer to last array element arr[0]
int * arrEnd = (arr + size - 1);
// Run loop till last array element
while(arr <= arrEnd)
{
scanf("%d", arr++);
}
}
/**
* Function to perform linear search in array. The function
* returns an integer between 0 to size-1 specifying first
* index of successful searched element in array.
*
* @arr Pointer to integer array.
* @size Size of the array.
* @toSearch Element to search within array.
*
* @return Returns -1 if element does not exists in array,
* otherwise returns element index.
*/
int search(int * arr, int size, int toSearch)
{
int index = 0;
// Pointer to last array element arr[size - 1]
int * arrEnd = (arr + size - 1);
/*
* Run a loop from start of array to last array element (arr <= arrEnd),
* until current array element does not match element to search.
*/
while(arr <= arrEnd && *arr != toSearch) {
arr++;
index++;
}
// If element is found
if(arr <= arrEnd)
return index;
return -1;
}
You can further optimize the above program by removing the unwanted index
variable from search()
function. Below is another implementation of search()
function without index
variable.
int search(int * arr, int size, int toSearch)
{
// Pointer to last array element arr[size - 1]
int * arrEnd = (arr + size - 1);
while(arr <= arrEnd && *arr != toSearch) arr++;
// If element is found
if(arr <= arrEnd)
return (arrEnd - arr) / sizeof(int) + 1;
return -1;
}
Output
Enter size of array: 10 Enter elements in array: 10 20 30 40 50 60 70 80 90 100 Enter element to search: 30 30 is found at 3 position.