CServer Side ProgrammingProgramming Show 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 − For example, p =&a;AlgorithmRefer 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
ProgramFollowing 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; }OutputWhen 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
Updated on 26-Mar-2021 07:19:19
C program to find the maximum number within n given numbers using pointers. C Program#include <stdio.h> #include <conio.h> void main() { int n,*p,i,h=0; clrscr(); printf("\nHOW MANY NUMBERS: "); scanf("%d",&n); p=(int *) malloc(n*2); if(p==NULL) { printf("\nMEMORY ALLOCATION UNSUCCCESSFUL"); exit(); } for(i=0;i<n;i++) { printf("\nENTER NUMBER %d: ",i+1); scanf("%d",(p+i)); } h=*p; for(i=1;i<n;i++) { if(*(p+i)>h) h=*(p+i); } printf("\nTHE HIGHEST NUMBER IS %d",h); getch(); }Output |