函数指针示例

目录:

代码:

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
相关推荐
Tisfy5 天前
LeetCode 1287.有序数组中出现次数超过25%的元素:遍历
算法·leetcode·题解·模拟·数组·遍历
中游鱼20 天前
C# 数组和列表的基本知识及 LINQ 查询
c#·linq·数组·数据处理·list数列
c++初学者ABC1 个月前
【例51.3】 平移数据
c++·数组
Tisfy1 个月前
LeetCode 2239.找到最接近 0 的数字:遍历
算法·leetcode·题解·数组·遍历
AQin10121 个月前
【Leetcode·中等·数组】59. 螺旋矩阵 II(spiral matrix ii)
算法·leetcode·矩阵·数组
WeeJot嵌入式2 个月前
C语言----数组
c语言·数组
Amd7942 个月前
特殊数据类型的深度分析:JSON、数组和 HSTORE 的实用价值
postgresql·json·数据存储·数据类型·数组·日期和时间·hstore
DogDaoDao2 个月前
leetcode 面试经典 150 题:删除有序数组中的重复项
算法·leetcode·面试·数组·双指针·数据结构与算法·重复数组
A懿轩A2 个月前
C/C++ 数据结构与算法【数组】 数组详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·数组
Moshow郑锴2 个月前
Spring Boot中CollectionUtils怎么用
springboot·数组·collectionutil