CServer Side ProgrammingProgramming
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,
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
- else
Program
Following is the C program to find the largest number in a series by using pointers −
Live Demo
#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 −
Updated on 26-Mar-2021 07:19:19
C program to find the maximum number within n given numbers using pointers.