Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
指针
|
155
指针
9.5 指向函数的指针
C
语言的函数指针有很多用法。例如,当调用一个函数时,可能不仅想传入该函数需要
被处理的数据,还希望传入指向子程序的指针,该子程序决定如何处理数据。我们刚刚
看过这种用途的例子:例
9-4
中使用的标准函数
qsort()
,它采用了一个指向比较函数的
指针作为它的一个参数,以对需要进行排序的数组进行比较操作。当需要比较数组中的
两个元素时,
qsort()
会应用指针来调用指定的比较函数。
也可以在数组中存储函数指针,然后使用数组的索引来调用这些函数。例如,键盘驱动
程序可能使用函数指针数组,数组索引对应到了键盘编码。当用户按下一个键时,程序
就会跳到对应的函数。
与数组指针的声明一样,函数指针的声明也需要括号。下面的例子展示了如何声明函
数指针。这个声明定义了一个指向函数的指针,该函数具有两个
double
类型的参数和
double
类型的返回值:
double (*funcPtr)(double, double);
该声明中采用括号将星号和标识符包围起来,这个括号很重要。如果没有它,声明
double *funcPtr(double, double);
则为函
数原型,而不是指针定义。
无论是否必须,函数名会被隐式地转换成函数指针。因此,下面的语句会将标准函数
pow()
的地址赋值给指针
funcPtr
,然后利用该指针调用这个函数:
double result;
funcPtr = pow; //
使得
funcPtr
指向函数
pow()
//
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207