原文: https://beginnersbook.com/2019/02/c-program-to-find-the-largest-of-three-numbers-using-pointers/
在本教程中,我们将编写一个 C 程序,使用指针查找三个输入数字中最大的一个。
使用指针查找最大数字的程序
在下面的程序中,我们有三个整数num1,num2,num3。我们已将这三个数字的地址分别赋给三个指针p1,p2,p3。之后我们使用if else语句对存储在指针指向的地址处的值进行了比较。
#include <stdio.h>int main(){int num1, num2, num3;int *p1, *p2, *p3;//taking input from userprintf("Enter First Number: ");scanf("%d",&num1);printf("Enter Second Number: ");scanf("%d",&num2);printf("Enter Third Number: ");scanf("%d",&num3);//assigning the address of input numbers to pointersp1 = &num1;p2 = &num2;p3 = &num3;if(*p1 > *p2){if(*p1 > *p3){printf("%d is the largest number", *p1);}else{printf("%d is the largest number", *p3);}}else{if(*p2 > *p3){printf("%d is the largest number", *p2);}else{printf("%d is the largest number", *p3);}}return 0;}
输出:

