通过在 C 语言中使用指针找出系列中的最大数
指针是一种存储另一个变量地址的变量。我们可以使用指针来保存空值。可以通过引用传递来访问它。另外,在声明变量时无需初始化。
指针的语法如下 −
pointer variable= & another variable;
例如,
p =&a;
算法
参考下面给出的算法,以便在指针的帮助下找出系列中的最大值。
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
使用指针来找出系列中最大值的 C 语言程序
以下是使用指针来找出系列中最大值的 C 语言程序 −
#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; }
输出
当执行上述程序时,将产生以下结果 −
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
广告