言归正传,我们来解析一系列的华为面试题。来看看我们的“功力”吧。
01. 请算出下面程序的输出:
/*
* ft1101.c
*
* Created on: 2012-11-5
* Author: xiaobin
*
* Huawei face questions
*/
typedef int(FUNC1) (int in);
typedef int(FUNC2) (int *, int *, int *);
int inc(int a) {
return (++a);
}
int muti(int *a, int *b, int *c) {
return (*c = (*a) * (*b));
}
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 argc, char* argv[])
{
int a = 0;
show(muti, 10, &a);
return 0;
}
开发环境:使用CDT+cygwin编写C/
C++
1. show函数 - 参数值
(1) fun = 乘积函数(muti)
(2)arg1 = 10
(3)arg2 = 0
2. show函数 - 内部执行
(1)指针p指向累加函数(inc)
(2)temp值等于执行完累加函数,即11
(3)执行乘积函数
参数1 :11
参数2:10
参数3:返回值
(4)输出上面乘积函数的参数3(返回值)
11 * 10 = 110