C program to search an element in array using pointers

Category: C Program
Tags: #cprogram#pointers#array#search

C program to search an element in array using pointers. This article provides step-by-step guidance and complete code examples, empowering programmers to enhance their understanding of array manipulation and pointer usage in C.

In C programming, arrays are fundamental data structures used for storing collections of elements. Implementing search algorithms to find specific elements within arrays is a common task, crucial for various applications. Leveraging pointers in C, we can efficiently traverse arrays and locate desired elements, offering dynamic and optimized solutions to the problem.

In this article, we'll explore how to search for an element in an array using pointers in C. Understanding this concept is essential for mastering array manipulation and memory management in the C programming language.

Understanding Pointers and Arrays in C

Before we delve into searching arrays using pointers, let's grasp the basics of pointers and arrays in C:

  • Arrays: Arrays in C are contiguous blocks of memory that store elements of the same data type. They provide a convenient way to organize and access collections of data.
  • Pointers: Pointers in C are variables that store memory addresses. They enable direct access to memory locations, allowing for efficient memory management and manipulation of data.

Searching Arrays using Pointers

To search for an element in an array using pointers in C, we'll iterate through the array elements using a pointer, comparing each element with the target element until a match is found or the end of the array is reached. Here's a step-by-step guide to searching arrays using pointers:

  1. Declare Array and Pointer: Define the array and a pointer to traverse the array elements.

     int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
     int target = 5; // Element to search for
     int *ptr = arr;
    
  2. Search for the Element using Pointers: Iterate through the array elements using the pointer, comparing each element with the target element.

    while (ptr < arr + SIZE) {
        if (*ptr == target) {
            printf("Element %d found at index %ld.\\n", target, ptr - arr);
            break;
        }
        ptr++;
    }
    
  3. Handle Element Not Found (Optional): If the element is not found, handle the case accordingly.

    if (ptr == arr + SIZE) {
        printf("Element %d not found in the array.\\n", target);
    }
    

Write a C program to search an element in array using pointers

Putting it all together, here's the complete C program to search for an element in an array using pointers:

#include <stdio.h>

#define SIZE 10

int main() {
    int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int target = 5; // Element to search for
    int *ptr = arr;

    // Search for the element using pointers
    while (ptr < arr + SIZE) {
        if (*ptr == target) {
            printf("Element %d found at index %ld.", target, ptr - arr);
            break;
        }
        ptr++;
    }

    // Handle element not found
    if (ptr == arr + SIZE) {
        printf("Element %d not found in the array.", target);
    }

    return 0;
}

Output

Element 5 found at index 4.

Recommended Posts