函数指针、函数指针数组、计算器+转移表等归纳总结

简介: 函数指针、函数指针数组、计算器+转移表等归纳总结

🔥🔥函数指针:指向函数的指针变量

🚵我们知道,数组名就是数组的首元素的地址,这样数组和指针就有了联系

那么,函数名也可以得到地址吗?

答案是:可以的,函数名得到的是函数的地址。

下面这俩种方式均可以取出函数的地址

&函数名

函数名

🚵书写格式

数组指针的书写格式是这样的

int arr[5]={0};

int (*p)[5]=&arr;

函数指针和数组指针类似;

int (*pf)(int,int)=&Add;

int ret=(*pf)(2,3);

或int ret=pf(2,3);

注意:有 * 就一定要有( )

🚵来看一个函数指针的简单代码,

#include <stdio.h>
int Add(int x, int y)
{
  return x + y;
}
void calc(int (*pf)(int, int))
{
  int x = 0;
  int y = 0;
  scanf("%d %d", &x, &y);
  int ret = (*pf)(x, y);
  printf("%d\n", ret);
}
int main()
{
  calc(Add);
  return 0;
}

具体剖析一下


🔥🔥函数指针数组:存放函数指针的数组

int (*arr[4])(int,int)={Add,Sub,Mul,Div};

int (*parr1[10])( );

用法尽在转移表中


🔥🔥🔥计算器 / 转移表

举例:写个计算器

这里介绍三种写法:普通方法、函数指针方法、函数指针数组方法

①普通的写法:

👇👇

#include <stdio.h>
void menu()
{
  printf("***************************\n");
  printf("***************************\n");
  printf("******    0.leave    ******\n");
  printf("******    1.Add      ******\n");
  printf("******    2.Sub      ******\n");
  printf("******    3.Mul      ******\n");
  printf("******    4.Div      ******\n");
  printf("***************************\n");
  printf("***************************\n");
}
int Add(int x, int y)
{
  return x + y;
}
int Sub(int x, int y)
{
  return x - y;
}
int Mul(int x, int y)
{
  return x * y;
}
int Div(int x, int y)
{
  return x / y;
}
int main()
{
  int input = 0;
  int a = 0;
  int b = 0;
  int c = 0;
  do
  {
    menu();
    printf("请选择:");
    scanf("%d", &input);
    switch (input)
    {
    case 0:
      printf("退出\n");
      break;
    case 1:
      printf("请输入俩个操作数:");
      scanf("%d %d", &a, &b);
      c = Add(a, b);
      printf("%d\n", c);
      break;
    case 2:
      printf("请输入俩个操作数:");
      scanf("%d %d", &a, &b);
      c = Sub(a, b);
      printf("%d\n", c);
      break;
    case 3:
      printf("请输入俩个操作数:");
      scanf("%d %d", &a, &b);
      c = Mul(a, b);
      printf("%d\n", c);
      break;
    case 4:
      printf("请输入俩个操作数:");
      scanf("%d %d", &a, &b);
      c = Div(a, b);
      printf("%d\n", c);
      break;
    default:
      printf("输入错误,重新输入\n");    
    }
  } while (input);
  return 0;
}

②用函数指针的写法:

👇👇

#include <stdio.h>
void menu()
{
  printf("***************************\n");
  printf("***************************\n");
  printf("******    0.leave    ******\n");
  printf("******    1.Add      ******\n");
  printf("******    2.Sub      ******\n");
  printf("******    3.Mul      ******\n");
  printf("******    4.Div      ******\n");
  printf("***************************\n");
  printf("***************************\n");
}
int Add(int x, int y)
{
  return x + y;
}
int Sub(int x, int y)
{
  return x - y;
}
int Mul(int x, int y)
{
  return x * y;
}
int Div(int x, int y)
{
  return x / y;
}
void calc(int (*pf)(int, int))
{
  int a = 0;
  int b = 0;
  int c = 0;
  printf("请输入俩个操作数:");
  scanf("%d %d", &a, &b);
  c = (*pf)(a, b);
  //c=pf(a,b);这种格式也可以
  printf("%d\n", c);
}
int main()
{
  int input = 0;
  do
  {
    menu();
    printf("请选择:");
    scanf("%d", &input);
    switch (input)
    {
    case 0:
      printf("退出\n");
      break;
    case 1:
      calc(Add);
      break;
    case 2:
      calc(Sub);
      break;
    case 3:
      calc(Mul);
      break;
    case 4:
      calc(Div);
      break;
    default:
      printf("输入错误,重新输入\n");    
    }
  } while (input);
  return 0;
}

🚵在这里我们来比较普通写法和函数指针写法:

🚵 通过上图,就可以发现普通写法实属冗余,函数指针的方法简化了很多。

但是函数指针需要写一个函数来接收函数的地址,如下:

③用函数指针数组的写法,又称转移表

👇👇

#include <stdio.h>
void menu()
{
  printf("***************************\n");
  printf("***************************\n");
  printf("******    0.leave    ******\n");
  printf("******    1.Add      ******\n");
  printf("******    2.Sub      ******\n");
  printf("******    3.Mul      ******\n");
  printf("******    4.Div      ******\n");
  printf("***************************\n");
  printf("***************************\n");
}
int Add(int x, int y)
{
  return x + y;
}
int Sub(int x, int y)
{
  return x - y;
}
int Mul(int x, int y)
{
  return x * y;
}
int Div(int x, int y)
{
  return x / y;
}
int main()
{
  int input = 0;
  int a = 0;
  int b = 0;
  int c = 0;
  do
  {
    menu();
    printf("请选择:");
    scanf("%d", &input);
    if (input == 0)
      printf("退出\n");
    else if (input >= 1 && input <= 4)
    {
      int (*p[5])(int x, int y) = { 0,Add,Sub,Mul,Div };
      printf("请输入俩个操作数:");
      scanf("%d %d", &a, &b);
      c = p[input](a, b);
      //c = (*p[input])(a, b);这种格式也可以
      printf("%d\n", c);
    }
    else
      printf("输入错误,重新输入\n");
  } while (input);
  return 0;
}

🚵这里我们来比较一下函数指针和函数指针数组:

这俩种方法并没有优劣,只是提供了不同的做题角度

具体剖析函数指针数组:

将Add、Sub、Mul、Div 函数的地址存放在函数指针数组里,用户输入input,再通过(*p[input])找到正确的函数地址


🚀结语:

如果对您有帮助的话,

不要忘记点赞+关注哦,蟹蟹

如果对您有帮助的话,

不要忘记点赞+关注哦,蟹蟹

如果对您有帮助的话,

不要忘记点赞+关注哦,蟹蟹

相关文章
|
3天前
|
编译器 vr&ar C语言
C primer plus 学习笔记 第10章 数组和指针
C primer plus 学习笔记 第10章 数组和指针
|
6天前
|
JSON Go 数据格式
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】(4)
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】
|
6天前
|
Java 编译器 Go
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】(3)
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】
|
6天前
|
存储 安全 Go
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】(2)
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】
|
6天前
|
Java Go 索引
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】(1)
Go 语言基础之指针、复合类型【数组、切片、指针、map、struct】
|
11天前
|
存储 C语言
C语言学习记录——7000+字长文-复习&学习指针(指针、地址、指针变量、指针与数组、指针与函数、指针数组、多级指针)二
C语言学习记录——7000+字长文-复习&学习指针(指针、地址、指针变量、指针与数组、指针与函数、指针数组、多级指针)二
12 1
|
11天前
|
存储 C语言
C语言学习记录——7000+字长文-复习&学习指针(指针、地址、指针变量、指针与数组、指针与函数、指针数组、多级指针)一
C语言学习记录——7000+字长文-复习&学习指针(指针、地址、指针变量、指针与数组、指针与函数、指针数组、多级指针)一
9 1
|
11天前
|
存储 C语言
C语言数组指针和指针数组的区别及使用方法
C语言数组指针和指针数组的区别及使用方法
11 0
|
17天前
|
存储 C语言
字符指针变量与字符数组的比较
字符指针变量与字符数组的比较
21 3
|
17天前
|
存储 C语言
指针数组作为main函数的形参
指针数组作为main函数的形参
7 0