
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to search an array element using Pointers.
Problem
Write a C program to search an element from an array at runtime by the user and the result to be displayed on the screen after search. If the searching element is not in an array then, we need to search element is not found.
Solution
An array is used to hold the group of common elements under one name
The array operations are as follows −
- Insert
- Delete
- Search
Algorithm
Refer an algorithm to search the elements into an array with the help of pointers −
Step 1 − Declare and read the number of elements.
Step 2 − Declare and read the array size at runtime.
Step 3 − Input the array elements.
Step 4 − Declare a pointer variable.
Step 5 − Allocate the memory dynamically at runtime.
Step 6 − Enter an element that to be searched.
Step 7 − Check if an element is present in an array by traversing. If an element is found display "Yes", otherwise "No".
Example
Size of array is: 5
The array elements are as follows:
1 2 3 4 5
Enter the element to be searched: 4
The output is as follows −
4 is present in the array
Example
Following is the C program to delete the elements into an array with the help of pointers −
#include<stdio.h> int i,l; int search(int ,int *,int); int main(){ int n,m; printf("enter the size of array:"); scanf("%d",&n); int a[n]; printf("enter the elements:
"); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("enter the element to be searched:"); scanf("%d",&m); search(n,a,m); return 0; } int search(int n,int *a,int m){ for(i=0;i<n;i++){ if(m==a[i]){ l=1; break; } } if(l==1){ printf("%d is present in the array",m); } else { printf("%d is not present in the array",m); } }
Output
When the above program is executed, it produces the following output −
Run 1: enter the size of array:5 enter the elements: 14 12 11 45 23 enter the element to be searched:11 11 is present in the array Run 2: enter the size of array:3 enter the elements: 12 13 14 enter the element to be searched:45 45 is not present in the array
- Related Articles
- C Program to insert an array element using pointers.
- C program to delete an array element using Pointers
- C++ Program to Find Minimum Element in an Array using Linear Search
- C++ Program to Find Maximum Element in an Array using Binary Search
- C++ Program to Find the Minimum element of an Array using Binary Search approach
- C++ Program to Find the peak element of an array using Binary Search approach
- Swift Program to Search an Element in an Array
- C++ program to search an element in a sorted rotated array
- Write a Golang program to search an element in an array
- Java Program to Recursively Linearly Search an Element in an Array
- Swift Program to Recursively Linearly Search an Element in an Array
- C Program to find sum of perfect square elements in an array using pointers.
- Recursive program to linearly search an element in a given array in C++
- C program to find array type entered by user using pointers.
- How to find minimum element in an array using linear search in C language?
