首页 > 试题广场 >

写出下列代码的输出内容()

[单选题]
写出下列代码的输出内容()
#include <stdio.h>
int inc(int a) { return (++a); }
int multi(int *a, int *b, int *c) { return (*c = *a * *b); }
typedef int (*FUNC1)(int in);
typedef int (*FUNC2)(int *, int *, int *);
void show(FUNC2 fun, int arg1, int *arg2) {
    FUNC1 p = &inc;
    int temp = p(arg1);
    fun(&temp, &arg1, arg2);
    printf("%d\n", *arg2);
}
int main() {
    int a;
    show(multi, 10, &a);
    return 0;
}

  • 100
  • 110
  • 120
  • 0
typedef int (*FUNC) (int )
或者
using FUNC = int (*) (int)
都是可以的。
发表于 2016-09-06 17:22:55 回复(0)
啥头像
函数指针用法错了

typedefint(FUNC1)(intin);
typedefint(FUNC2)(int*,int*,int*);
这两句应该改成
typedefint(*FUNC1)(intin);
typedefint(*FUNC2)(int*,int*,int*);
发表于 2015-09-05 10:57:31 回复(3)
Ze头像 Ze
typedefint(FUNC1)(intin); 是函数指针定义

show(multi,10,&a); FUNC2类型函数指针fun 指向函数multi的首地址
FUNC1 p=&inc;  FUNC1类型 函数指针p 指向函数inc的首地址
int temp=p(arg1); 此时调用函数inc,参数为10,返回值为11
fun(&temp,&arg1,arg2); 调用函数multi,参数为(11,10,arg2) arg2为指针变量负责带回返回值
printf("%d\n",*arg2); 输出 110
编辑于 2015-09-03 16:33:46 回复(4)
在 visual studio 下运行该程序有个编译错误:FUNC1 p=&inc;
显然这里想把p 定义为指向函数的指针,但是这里把p 定义为一个函数了;
应该把typedefint(FUNC1)(intin); 改为typedefint(*FUNC1)(intin);
或者FUNC1 p=&inc;改为 FUNC1 *p=&inc;
各位同学可以试一下!
我用的是C++编译器,不知道C 下是否能按题目说的那样定义函数指针。
编辑于 2015-09-04 17:57:44 回复(8)
nez头像 nez
惭愧了,大学四年都没用过这样的语法……
发表于 2018-03-14 22:22:41 回复(1)
typedefint(FUNC1)(intin); 是函数指针定义 show(multi,10,&a); FUNC2类型函数指针fun 指向函数multi的首地址 FUNC1 p=&inc;  FUNC1类型 函数指针p 指向函数inc的首地址 int temp=p(arg1); 此时调用函数inc,参数为10,返回值为11 fun(&temp,&arg1,arg2); 调用函数multi,参数为(11,10,arg2) arg2为指针变量负责带回返回值 printf("%d\n",*arg2); 输出 110
发表于 2021-11-02 10:09:21 回复(0)
先把题目改对了,函数指针写漏了*,第14行,改成
 FUNC1* p = &inc;

发表于 2017-03-29 22:18:47 回复(1)
题目错误,加两个*, 函数指针p指向函数inc,p(arg1)即为p指向的函数以arg1为实参,即temp=inc(10)=11,同理,函数指针fun作为形参,其实参为multi,所以等价于multi(11 ,10,arg2),输出arg2=11×10=110。
发表于 2015-11-07 00:15:25 回复(0)
链接:https://www.nowcoder.com/questionTerminal/2c18fc889b924cc1b21ce6ec387fd853?examPageSource=%E4%B8%93%E9%A1%B9%E7%BB%83%E4%B9%A0%E7%AD%94%E6%A1%88#jsEditorModuleBody
来源:牛客网
2    int inc(int a) { return (++a); }

4    typedef int (*FUNC1)(int in);

7    FUNC1 p = &inc;

第七行改成  FUNC1 p = inc; 也可以运行,结果相同。
我的理解是当p = &inc时,inc是函数名,&inc则是函数地址,类似于通过&x(x为int类型变量)取x地址;当p = inc时,inc是函数地址,类似于通过数组名取首元素地址。C中好像是有这种情况,欢迎指正。
    
发表于 2023-07-12 02:30:26 回复(0)
注意在函数内,或函数指针内的值,++a,不影响函数外的a,就可以了,有typedef 没有typedef,都是这么一回事。
发表于 2023-06-22 17:46:20 回复(0)
***题
发表于 2023-03-01 07:52:00 回复(0)
看评论又在本地运行了一下,没报错啊,输出110
发表于 2022-09-28 18:01:17 回复(0)
讨厌这种题
发表于 2022-03-01 18:01:13 回复(0)
*fun 指向了 multi函数的地址 fun成了一个函数指针 int *型
*p 指向了 inc函数的地址 p也成了一个函数指针 int型
trmp =  p(10)  进入函数inc中 ++a ,a= 11 返回回来
temp = 11;&arg1 == a == 10;
fun 相当于 multi函数 进行了 ;里面 把10 * 11 = 110
最后返回了 110
发表于 2021-07-17 11:44:47 回复(0)
知道影响实参就够了
发表于 2019-12-01 20:12:10 回复(0)
c4可以编译droid
发表于 2017-11-08 20:51:00 回复(0)
B
发表于 2016-03-12 21:14:38 回复(0)
这道题主要理解传值得问题,inc函数执行后a的值是没有发生改变的
发表于 2015-12-26 21:02:31 回复(0)
//FUNC1和FUNC2都是函数类型
typedefint(FUNC1)(intin);
typedefint(FUNC2)(int*,int*,int*);
如果此处不修改,需要修改代码第14行,修改后代码如下 FUNC1 *p = &inc;  或者  FUNC1 *p = inc;  这两种初始化的方式都是合法的。
  如果修改代码第10行和第11行,代码如下:
  //FUNC1和FUNC2都是指向函数的指针
  typedef int (*FUNC1)(int in);
  typedef int (*FUNC2)(int*,int*,int*);
  代码第14行不需要做任何修改。

发表于 2015-09-25 00:17:12 回复(1)
使用Qt时,以上程序编译错误,需要改为
typedef int (*FUNC1)(int in);
typedef int (*FUNC2)(int*,int*,int*);
发表于 2015-09-13 10:28:09 回复(0)