关于一道华为面试题目的疑问

stecdeng 2006-07-10 08:55:27
在社区帖子里看到一道面试的帖子
由于我是新人 有些看不懂
在此提问 希望大家指教



5、写出下列代码的输出内容
#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)
{
INCp=&inc;
int temp =p(arg1);
fun(&temp,&arg1, arg2);
printf("%d\n",*arg2);
}

main()
{
int a;
show(multi,10,&a);
return 0;
}
答:110



**************
请问这两句如何解释?
typedef int(FUNC1)(int in);
typedef int(FUNC2) (int*,int*,int*);
这个在编译时候出现问题
INCp=&inc;

INCp是指针?



谢谢

...全文
1288 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggyg1121 2006-07-13
  • 打赏
  • 举报
回复
typedef int(FUNC1)(int int);
typedef int(FUNC2) (int*,int*,int*);
这是声明函数类型的,这个声明以后,可以用FUNC1来声明返回为整型,参数为两个int型的函数,
如: FUNC1 inc;则函数inc就是一个返回为整型,参数为两个int型的函数.
第二行意思相同.
paobo 2006-07-11
  • 打赏
  • 举报
回复
学习一下
ajieyxw 2006-07-11
  • 打赏
  • 举报
回复

void show(FUNC2 fun,int arg1, int*arg2)
{
int (*p)(int)=&inc;
//INCp=&inc;
int temp =p(arg1);
fun(&temp,&arg1, arg2);
printf("%d\n",*arg2);
}
kyozpeng 2006-07-11
  • 打赏
  • 举报
回复
嗯,以前只见过typedef int(FUNC1)(int int)这种方式申明来调用.dll中的方法,原来还可以这样利用.
manplus 2006-07-11
  • 打赏
  • 举报
回复
mr
visual_alan 2006-07-11
  • 打赏
  • 举报
回复
mark
laiwusheng 2006-07-10
  • 打赏
  • 举报
回复
typedef int(FUNC1)(int int);//FUNC1是被自定义为int ()(int in)函数指针类型的类型
WingForce 2006-07-10
  • 打赏
  • 举报
回复
typedef int(FUNC1)(int int);
typedef int(FUNC2) (int*,int*,int*);

声明函数指针类型

FUNC1类型的东东是一个指向型如如

int func_foo(int a, int b);

的函数实体的指针
stecdeng 2006-07-10
  • 打赏
  • 举报
回复
INCp在DEC C++下运行出现
main.c `INCp' undeclared (first use in this function)
提示


C++PRIMER上
只有
TYPEDEF DOUBLE WAGES;
TYPEDEF INT *PINT;
几个简单的例子
至于
typedef int(FUNC1)(int in);
typedef int(FUNC2) (int*,int*,int*);
难道是为INT引入(FUNC1)(int in)助记类型?

谢谢



jixingzhong 2006-07-10
  • 打赏
  • 举报
回复
INCp=&inc;
==》
FUNC1 p =&inc;

这里应该是定义 函数指针 p ...

看主函数,传如 show 的10, 和 a,
show 中先调用 p 函数,指向了 inc 函数,运算使得 a = 10++ = 11,
然后调用multi 函数, 结果是 10*11 =110
jixingzhong 2006-07-10
  • 打赏
  • 举报
回复
请问这两句如何解释?
typedef int(FUNC1)(int in);
typedef int(FUNC2) (int*,int*,int*);

这个是 指针 别名定义
stecdeng 2006-07-10
  • 打赏
  • 举报
回复
YeTimmy() ( ) 信誉:100 2006-7-10 14:25:11 得分: 0



函数指针可以这样定义吗???
DEV下通不过。。
typedef int(*FUNC1)(int in);
typedef int(*FUNC2) (int*,int*,int*);
要这样写我这才通的过:(



一个指针一个引用 一个PRINTF 一个 COUT啊
stecdeng 2006-07-10
  • 打赏
  • 举报
回复
多谢各位
总算是明白了

to cxyol(C++,VC 学习中......) ( ) 信誉:98
我也笔错误
是FUNC1没出现


haha168_2002 2006-07-10
  • 打赏
  • 举报
回复
终于懂了,谢谢大家,我看这题的时候也看得一头雾水。
YeTimmy 2006-07-10
  • 打赏
  • 举报
回复
函数指针可以这样定义吗???
DEV下通不过。。
typedef int(*FUNC1)(int in);
typedef int(*FUNC2) (int*,int*,int*);
要这样写我这才通的过:(
happytang 2006-07-10
  • 打赏
  • 举报
回复
明显原题有错误,可能是笔误
INCp=&inc;无意义
改称:
FUNC1* p = inc;
int temp =p(arg1);

分析一下
==================================
#include <iostream>
using namespace std;

// 返回a + 1
int inc(int a){
return a+1;
}
// out = a*b
int multi(int& a, int& b, int& out){
return out = a*b;
}

// 定义两种函数,FUNC1对应inc, FUNC2对应 multi
typedef int(FUNC1) (int in);
typedef int(FUNC2) (int&,int&,int&);

FUNC1* p = inc;//原文这句写错了...orz... INC根本没定义
int numAdd1 = p( num );
// 等价于 numAdd1 = inc( num );
// 结果 num仍然是10, numAdd1 = 11

// 调用func函数,可以看到, main中传给func的是 multi.
// 所以调用的是 multi( 11, 10, out ), out = 110
func( numAdd1, num, out );
cout<<out<<endl;
}

int main(){
int a;
show(multi,10, a);
return 0;
}
==================================
cxyol 2006-07-10
  • 打赏
  • 举报
回复
TO: WingForce(初六,履霜,坚冰至。)
typedef int(FUNC1)(int int);
typedef int(FUNC2) (int*,int*,int*);

声明函数指针类型

FUNC1类型的东东是一个指向型如如

int func_foo(int a, int b);的函数实体的指针

------------------
原文中的typedef int(FUNC1)(int in);没有错吧。
要是指向int func_foo(int a, int b);的话应该是:
typedef int(FUNC1)(int ,int);注意里面的逗号呀!

-----------------------------------------------
TO:stecdeng()

func2为何没出现 //应该是FUNC2,注意大小写呀
typedef int(FUNC1)(int in); IN是是否是INT少写了T字母
*******************************
void show(FUNC2 fun,int arg1, int*arg2) 这里不是出现了?

不是少写了。
laiwusheng 2006-07-10
  • 打赏
  • 举报
回复
为何出现main.c `INCp' undeclared (first use in this function)
提示
也是笔误?
====================================
//确实笔误,应为:FUNC1 *p=inc;
//inc是int inc(int a);这个函数的地址;
//改了一下,运行成功!

#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);
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);
}

main()
{
int a;
show(multi,10,&a);
getch();
return 0;
}
stecdeng 2006-07-10
  • 打赏
  • 举报
回复
func2为何没出现
typedef int(FUNC1)(int in); IN是是否是INT少写了T字母
******************
INCp=&inc;
==》
FUNC1 p =&inc;

这里应该是定义 函数指针 p ...
为何出现main.c `INCp' undeclared (first use in this function)
提示
也是笔误?

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧