Search an element in the array using pointers
C program to search an element in the array using pointers
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void input_array(int arr[], int n);
void main()
{
int arr[MAX];
int n, i, num, flag = 0;
int *ptr;
printf("Enter the size of the array\t:");
scanf("%d",&n);
printf("\nEnter elements of the array\n");
input_array(arr, n);
printf("\nEnter the element to be searched\t:");
scanf("%d",&num);
ptr = arr; //pointer ptr points to arr[0]
for(i=0;i<n;i++)
{
if(*ptr == num)
{
printf("\nElement found at position %d",i+1);
flag = 1;
}
ptr++;
}
if(flag == 0)
{
printf("\nElement not found");
}
getch();
}
void input_array(int *arr, int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}
Output
Enter the size of the array :5
Enter elements of the array
1
3
5
3
1
Enter the element to be searched :3
Element found at position 2
Element found at position 4
Explanation
In the above program, we have to search an element in the array using pointers. For this purpose, number of elements in the array 'n' and elements of the array 'arr' are taken as an input from the user. pointer 'ptr' is declared and points to array 'arr'.
To input the arrays, input_array() function is used. This function takes array 'arr' and number of elements in the array 'n' as arguments
To search the element, each element is traversed and compared with the number entered by the user 'num'.
Initially 'ptr' points to 'arr[0]'.
Element of the array pointed by 'ptr' is compared to 'num'. If both are same, then element is found and 'flag' variable is set to 1.
If match does not occur then, we have to check for the next element. For this, ptr will be incremented by 1.
The above process is repeated 'n' times, i.e. for all the elements of the array.
'flag' is the variable taken to keep track of whether the element is found or not. 'flag' is initialized to 0.
If element is found in the array then, 'flag' is set to 1.
If element is not found, then the value of 'flag' will not change and will remain 0.
So, at the end of loop, we will check whether the value of 'flag' is 0 or not.
If it is 0, then message "element not found" will be printed
Note that, if the element is found, we are not exiting the loop there itself. This means, all the occurrences of 'num' are searched and printed.
ptr = arr;
To input the arrays, input_array() function is used. This function takes array 'arr' and number of elements in the array 'n' as arguments
To search the element, each element is traversed and compared with the number entered by the user 'num'.
Initially 'ptr' points to 'arr[0]'.
Element of the array pointed by 'ptr' is compared to 'num'. If both are same, then element is found and 'flag' variable is set to 1.
If match does not occur then, we have to check for the next element. For this, ptr will be incremented by 1.
ptr++;
The above process is repeated 'n' times, i.e. for all the elements of the array.
'flag' is the variable taken to keep track of whether the element is found or not. 'flag' is initialized to 0.
If element is found in the array then, 'flag' is set to 1.
If element is not found, then the value of 'flag' will not change and will remain 0.
So, at the end of loop, we will check whether the value of 'flag' is 0 or not.
If it is 0, then message "element not found" will be printed
Note that, if the element is found, we are not exiting the loop there itself. This means, all the occurrences of 'num' are searched and printed.