函数指针示例

目录:

代码:

main.c

c 复制代码
#include <stdio.h>
#include <stdlib.h>

int Max(int x, int y);
int Min(int x, int y);

int main(int argc, char**argv)
{
    int x,y;
    scanf("%d",&x);
    scanf("%d",&y);

    int select;
    printf("输入1表示求最小值,2表示最大值\n");
    scanf("%d",&select);
    //定义函数指针
    int (*p)(int, int);
    if(select == 1)
    {
        //最小值
        p = Min;
    }
    else if(select == 2)
    {
        //最大值
        p =Max;
    }
    else
    {
        return -1;
    }
    //查找
    printf("value is %d\n", p(x, y));
}

int Max(int x, int y)
{
    return x > y?x:y;
}
int Min(int x, int y)
{
    return x < y?x:y;
}

Makefile

c 复制代码
main:main.c
	gcc -o $@ $^
	./$@
clean:
	rm main
相关推荐
闻缺陷则喜何志丹1 天前
【C++动态规划 状压dp】1879. 两个数组最小的异或值之和|2145
c++·算法·动态规划·力扣·数组·最小·动态规范
故事不长丨3 天前
C#数组深度解析:从基础语法到实战应用
开发语言·c#·数组·array
kylezhao20197 天前
C# 数组核心全面详解
c#·数组
2401_841495649 天前
【LeetCode刷题】寻找重复数
数据结构·python·算法·leetcode·链表·数组·重复数
2401_8414956414 天前
【LeetCode刷题】零钱兑换
数据结构·python·算法·leetcode·动态规划·数组·时间复杂度
2401_8414956415 天前
【LeetCode刷题】打家劫舍
数据结构·python·算法·leetcode·动态规划·数组·传统dp数组
Irene199115 天前
JavaScript 字符串和数组方法总结(默写版:同9则6 Str21 Arr27)
javascript·字符串·数组·方法总结
l1t16 天前
在postgres和duckdb中比较两个数组并只挑选不匹配的元素
数据库·sql·postgresql·数组·duckdb
B_lack02617 天前
字节转换算法应用_读取本地时间
数据结构·算法·数组·西门子plc·博途·时间处理·scl