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.
The syntax for pointer is as follows −
pointer variable= & another variable;
For example,
p =&a;
Algorithm
Refer an algorithm given below for finding the largest number in a series with the help of 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
Program
Following is the C program to find the largest number in a series by using pointers −
#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