C Program to search an element in an array using Pointers2 min read

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array will be passed. The function will return 1 if any element matches with the searched element otherwise it will return 0.

This C program defines a function called search_function that searches for a given element in an array. Here’s a concise explanation of the program:

  1. Function Definition: search_function(int *a, int n, int s_element)
    • This function takes three parameters:
      • a: a pointer to an integer array.
      • n: the size of the array.
      • s_element: the element to be searched in the array.
    • It iterates through the array using a for loop and checks if the current element is equal to the s_element.
    • If it finds a match, it returns 1 (true), indicating that the element is present in the array.
    • If no match is found after checking all elements, it returns 0 (false).
  2. Main Function: main()
    • The main function begins the execution of the program.
    • It declares an array arr of size 30, variables i (for loop iteration), size (to store the size of the array), and s_element (the element to be searched).
    • The user is prompted to enter the size of the array, followed by the array elements.
    • The user is then prompted to enter the element to be searched (s_element).
    • The search_function is called with the array, size, and search element, and the result is used to print whether the element is present or not.
  3. Output:
    • The program outputs whether the searched element is present or not in the array.

MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …