笔试题
来源:互联网 发布:matlab 未知数矩阵 编辑:程序博客网 时间:2024/06/06 08:32
转载自:http://blog.csdn.net/fly__chen/article/details/52741473
今天做到一题面试题如下, 读到第9行的时候发现和自己理解的有出入。
个人理解函数名是函数的入口地址就是一个人指针,那为什么还要取地址再赋值给p呢?
带着疑惑百度了一番,发现网上没人发现程序中的问题,所以就记录一下,希望以后做到这题的同学不再被误解。
错误
//代码为网上复制的,存在许多问题//运行的结果提示: 程序中有游离的‘\343’等一系列错误#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("%dn",*arg2); } main() { int a; show(multi,10,&a); return 0;}
于是自己手打了一遍:
#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;}//编译结果错误为://test.c: 在函数 ‘show’ 中://test.c:26: 错误:函数 ‘p’ 像变量一样被初始化
分析:
FUNC1 p = &inc;
左: 定义一个形参为int型,返回值为int型的函数p(p为函数的入口地址)。
右: 将函数inc()的入口地址 取 地址;
左右明显是不同的数据类型。
修改
//方法一:#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); //此时FUNC1变成了指向函数的指针typedef int(FUNC2)(int *,int *,int *);void show(FUNC2 fun,int arg1,int * arg2){ FUNC1 p = &inc; // FUNC1 p = inc; 这样写也是对的,不知道如何理解。// 个人理解: 像int a[10]一样 , a和&a 是一个地址 int temp = p(arg1); fun(&temp,&arg1,arg2); printf("%d\n",*arg2);}int main(){ int a; show(multi,10,&a); return 0;}
//方法二:#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; //定义一个指针p指向一个形参为int型,返回值为int型的函数 int temp = p(arg1); fun(&temp,&arg1,arg2); printf("%d\n",*arg2);}int main(){ int a; show(multi,10,&a); return 0;}
问题
//为什么在typedef int(FUNC2)(int *,int *,int *);的FUNC前加*程序也是对的//请看到这篇文章的同学,帮我一下,谢谢。//如下:#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; //定义一个指针p指向一个形参为int型,返回值为int型的函数 int temp = p(arg1); fun(&temp,&arg1,arg2); printf("%d\n",*arg2);}int main(){ int a; show(multi,10,&a); return 0;}
阅读全文
0 0
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 笔试题
- 阿里云ecs windows 2008 安装ftp
- 同一进程的线程共享的资源和独有的资源
- leetcode 287. Find the Duplicate Number
- 解题报告:Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers 暴力
- 目标检测-- DeNet: Scalable Real-time Object Detection with Directed Sparse Sampling
- 笔试题
- 九度机试笔记20170903---2
- MySQL高性能表设计规范
- git使用笔记
- Linux命令
- 100. Same Tree
- 轨道交通智能化系统的架构与优势
- Java 并发工具包
- solr建立索引