C Program to Search an Element in an Array Using Pointers

Introduction

Searching for an element in an array involves checking whether a specific value is present in the array and, if so, finding its position. In C, this can be done using pointers to traverse the array and compare each element with the target value. This guide will show you how to write a C program to search for an element in an array using pointers.

Example:

  • Input: Array [5, 2, 9, 1, 5, 6], Search element 5
  • Output: Element 5 found at position 1 and 4

Problem Statement

Create a C program that:

  • Takes an array of integers and a target element as input from the user.
  • Uses pointers to search for the target element in the array.
  • Displays the position(s) of the element if found or indicates that the element is not present.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Declare the Array, Pointer, and Other Variables: Declare an integer array to store the elements, a pointer for traversal, and other variables for input and control.
  3. Input the Array Elements: Use a loop to take input for the array elements from the user.
  4. Input the Element to Search: Use scanf to take input for the element to search.
  5. Search for the Element Using Pointers: Use pointers to traverse the array and compare each element with the target value.
  6. Display the Result: Use printf to display the position(s) of the element if found or indicate that the element is not present.

C Program to Search for an Element in an Array Using Pointers

#include <stdio.h>

int main() {
    // Step 2: Declare the array, pointer, and other variables
    int arr[100], n, target;
    int *ptr;
    int found = 0;

    // Step 3: Input the number of elements and the array elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    printf("Enter the elements of the array: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Step 4: Input the element to search for
    printf("Enter the element to search: ");
    scanf("%d", &target);

    // Step 5: Search for the element using pointers
    ptr = arr;  // Initialize pointer to the start of the array

    for (int i = 0; i < n; i++) {
        if (*ptr == target) {
            printf("Element %d found at position %d\n", target, i);
            found = 1;  // Set found flag to true
        }
        ptr++;  // Move the pointer to the next element
    }

    if (!found) {
        printf("Element %d not found in the array.\n", target);
    }

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Declare the Array, Pointer, and Other Variables

  • The integer array arr is declared to store the elements of the array.
  • The pointer ptr is used to traverse the array.
  • The variables n and target are used to store the number of elements and the target element, respectively.
  • The variable found is used as a flag to indicate whether the element is found in the array.

Step 3: Input the Array Elements

  • The program prompts the user to enter the number of elements in the array and stores this value in n.
  • A for loop is used to take input for each element of the array.

Step 4: Input the Element to Search

  • The program prompts the user to enter the element to search for in the array using scanf.

Step 5: Search for the Element Using Pointers

  • The pointer ptr is initialized to point to the first element of the array (arr).
  • A for loop is used to traverse the array:
    • The loop compares the element pointed to by ptr with the target element.
    • If the target element is found, the program prints its position and sets the found flag to 1.
    • The pointer ptr is incremented to move to the next element in the array.
  • After the loop, if the found flag is still 0, the program indicates that the element was not found.

Step 6: Display the Result

  • If the element is found, the program displays its position(s).
  • If the element is not found, the program prints a message indicating that the element is not present in the array.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output 1:

Enter the number of elements: 6
Enter the elements of the array: 5 2 9 1 5 6
Enter the element to search: 5
Element 5 found at position 0
Element 5 found at position 4

Example Output 2:

Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Enter the element to search: 7
Element 7 not found in the array.

Conclusion

This C program demonstrates how to search for an element in an array using pointers. It covers basic concepts such as pointer manipulation, array traversal, and conditional checking, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top