函数指针示例

目录:

代码:

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
相关推荐
2401_841495642 天前
【LeetCode刷题】跳跃游戏
数据结构·python·算法·leetcode·游戏·贪心算法·数组
2401_841495643 天前
【LeetCode刷题】缺失的第一个正数
数据结构·python·算法·leetcode·数组·哈希·缺失最小正整数
利刃大大4 天前
【JavaSE】十、ArrayList && LinkedList
java·链表·数组
2401_841495644 天前
【LeetCode刷题】轮转数组
数据结构·python·算法·leetcode·数组·双指针·轮转数组
蜗牛攻城狮11 天前
JavaScript `Array.prototype.reduce()` 的妙用:不只是求和!
前端·javascript·数组
2401_841495641 个月前
【LeetCode刷题】找到字符串中所有字母异位词
数据结构·python·算法·leetcode·数组·滑动窗口·找到字符串中所有字母异位词
alwaysrun1 个月前
Rust中数组简介
rust·数组·array·切片
2401_841495641 个月前
【LeetCode刷题】移动零
数据结构·python·算法·leetcode·数组·双指针法·移动零
暴风鱼划水1 个月前
算法题(Python)数组篇 | 6.区间和
python·算法·数组·区间和
課代表1 个月前
VB.Net 常用函数
字符串·类型转换·数组·函数·vb.net·日期时间·条件