C Program to Search an Element in an Array Using Pointer and 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 array element: ");
    for (i=0;i<5;i++)
    {
        scanf("%d",pa+i);
    }

    printf("Enter element for 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.\n");

    else
        printf("Not found.\n");
}

Output:-

Enter array element: 12
13
14
15
16
Enter element for search: 9
Not found.

Enter array element: 25
32
61
48
91
Enter element for search: 32
Found.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Similar C programs on Pointers

Leave a Reply