Search Element in Array Using Pointer & Function in C

C Program to Search an Element in an Array Using Pointer and Function | A pointer is a variable whose value is the address of another variable. A function is a block of code that performs a specific task. For example, the main is function and every program execution starts from the main function in C programming. Here, We develop a program to search element in an array using pointer & function in c.

#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 Comment

Your email address will not be published. Required fields are marked *