Learn about Search Element in Array Using Pointer & Function in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Search Element in Array Using Pointer & Function in C
C Program to Find an Array Element Using a Pointer and a Function . A variable whose value is the address of another variable is referred to as a pointer. A function is a piece of code that accomplishes a specified goal. In C programming, for example, the main is function, and every program execution begins with the main function. In this section, we will create a program in C that will search for an element in an array using a pointer and a function.
#include<stdio.h>
void search(int *x, int *y);
int main()
{
int a[5], n, i;
int *pa, *pn;
pa = &a[0], pn = &n;
printf("Enter elements of array: ");
for (i=0;i<5;i++)
{
scanf("%d",pa+i);
}
printf("Enter element to search: ");
scanf("%d",pn);
search(&a[0],pn);
return 0;
}
void search (int *x, int *y)
{
int i, f=0;
for (i=0;i<5;i++)
{
if ( *(x+i) == *y)
{
f=1;
break;
}
else
continue;
}
if (f==1)
printf("Found in array\n");
else
printf("Not found in array\n");
}
Output:
Enter elements of array: 6
90
87
5
45
Enter element to search: 6
Found in array
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C Program to Sort List of Array Elements Using Function & Pointer
Find Sum and Average of an Array Using the Pointer in C