
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Largest Number in a Series Using Pointers in C Language
A pointer is a variable that stores the address of another variable. We can hold the null values by using pointer. It can be accessed by using pass by reference. Also, there is no need of initialization, while declaring the variable. Instead of holding a value directly, it holds the address where the value is stored in memory. The two main operators used with pointers are the dereferencing operator and the address operator. These operators are used to declare pointer variables and access the values stored at those address.
Pointer variable uses the dereferencing operator to access the value it points to. We use the address operator to obtain the memory address of a variable and store it in the pointer variable.
We use pointers to access the memory of the variable, and then this manipulates their address in a program. The pointers are very different features that provide the language with flexibility and power.
The syntax for the pointer is as follows ?
pointer variable= & another variable;
This code assigns the address of the variable a to the pointer p, making p point to a.
p =&a;
Algorithm
Refer to the algorithm below for finding the largest number in a series using pointers.
-
Step 1: Start
-
Step 2: Declare integer variables
-
Step 3: Declare pointer variables
-
Step 4: Read 3 numbers from console
-
Step 5: Assign each number address to pointer variable
-
Step 6: if *p1 > *p2 if *p1 > *p3 print p1 is large else print p2 is large
-
Step 7: Else if *p2 > *p3 Print p2 is large Else Print p3 is large
-
Step 8: Stop
Example
Here is a C program, to find the largest number in a series using pointers. This code reads three integers, assigns their addresses to pointers, and compares the values using the pointers to determine and print the largest number among them.
#include <stdio.h> int main() { int num1, num2, num3; int *p1, *p2, *p3; printf("enter 1st no: "); scanf("%d", &num1); printf("enter 2nd no: "); scanf("%d", &num2); printf("enter 3rd no: "); scanf("%d", &num3); p1 = &num1; p2 = &num2; p3 = &num3; if (*p1 > *p2) { if (*p1 > *p3) { printf("%d is largest ", *p1); } else { printf("%d is largest ", *p3); } } else { if (*p2 > *p3) { printf("%d is largest ", *p2); } else { printf("%d is largest ", *p3); } } return 0; }
Output
When the above program is executed, it produces the following result ?
Run 1: enter 1st no: 35 enter 2nd no: 75 enter 3rd no: 12 75 is largest Run 2: enter 1st no: 53 enter 2nd no: 69 enter 3rd no: 11 69 is largest