指向函数的指针(的数组)实例总结

2015年12月06日 其他资源 10条评论 阅读3657次

指向函数的指针(的数组)实例总结


前言

C语言、C++、ObjectC中都有类似指向函数的指针(的数组)的概念,用起来还是很不错的。运用得当能起到事半功倍的效果。

百度百科中“指向函数的指针”的定义

函数指针是指向函数的指针变量。 因而“函数指针”本身首先应是指针变量,只不过该指针变量指向函数。这正如用指针变量可指向整型变量、字符型、数组一样,这里是指向函数。如前所述,C在编译时,每一个函数都有一个入口地址,该入口地址就是函数指针所指向的地址。有了指向函数的指针变量后,可用该指针变量调用函数,就如同用指针变量可引用其他类型变量一样,在这些概念上是大体一致的。函数指针有两个用途:调用函数和做函数的参数。

指向函数的指针

根据前面看到在百科中对指向函数的指针的解释,我们能够知道这其实就是一个指针的概念,只不过这个指针并不是指向一个数据类型,而是指向一个函数的。

这里可以借鉴指向基本数据类型的只针对概念对此理解。 指向int类型的指针变量可以这样声明

int a;
int *p;

对上面声明的p指针可以这样初始化

p = &a;

使用p指针的方法:

*p = 10;

上述代码就通过p指向的地址把变量a设置为10。

通过上面对指针int类型的指针的回顾,下面开始指向函数的指针

设想有如下函数:

int process(const char *s, int value);

只是一个例子而已,不用管实现神马功能。 如果不用指向函数的指针,直接调用应该是下面这样子

int result = process("jileniao.net", 100);

如果换做指向函数的指针方法,则会看到如下的代码。

int(*p_func)(const char *s, int value);
p_func = process; 

实际调用时,将会是:

int result = (*p_func)("jileniao.net", 100);

记住指向函数的指针在使用时,原函数名就是在内存中所占的地址(相当于取地址符&)。

指向函数的指针的数组

本概念,结尾词落在数组上,说明无论前面是神马东西,最终这必然是一个数组,而数组中的每个元素就是上面所说的指向函数的指针 。

知道了这个本质,用起来也就没有太大的问题了。

请看实例:

int process1(const char *s, int value);
int process2(const char *s, int value);
int process3(const char *s, int value);
int process4(const char *s, int value);
int process5(const char *s, int value);
int process6(const char *s, int value);
int process7(const char *s, int value);
int process8(const char *s, int value);
int process9(const char *s, int value);

如上,有若干同样参数,同样返回值的一系列函数,想要在某种情况下依次调用,不通过指向函数的指针的数组,可以顺序写一遍就OK了。但若想做的更优雅一些,指向函数的指针的数组就可以派上用场了。

int (*p_funcions[9])(const char *s, int value);

上面就已经声明了一个数组长度为9的指向函数的指针的数组,继续往下看是对其初始化。

p_funcions[0] = process1;
p_funcions[1] = process2;
p_funcions[2] = process3;
p_funcions[3] = process4;
p_funcions[4] = process5;
p_funcions[5] = process6;
p_funcions[6] = process7;
p_funcions[7] = process8;
p_funcions[8] = process9;

想要使用这个数组,则可以这样。

int results[9];
for (int i = 0; i < 9; ++i) {
    results[i] = (*p_funcions[i])("jileniao.net", 100);
}

文末,欢迎留言提出意见。谢谢!


分享本文至:

WRITTEN BY

avatar
本文标签:指针函数指针
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

10 条评论

  1. avatar incommonblog

    <a href="http://www.houstonujc.com/mens-nike-new-york-giants-17-kyle-lauletta-limited-olive-2017-salute-to-service-nfl-jersey-nflr">mens nike new york giants 17 kyle lauletta limited olive 2017 salute to service nfl jersey</a> <a href="http://www.jetsonpress.com/nike-air-force-1-olive-gr%C3%BCn-butyg">nike air force 1 olive gr眉n</a> <a href="http://www.masadablog.com/nfl-marqise-lee-womens-game-teal-green-jersey-11-jacksonville-jaguars-nike-alternate-nflz">nfl marqise lee womens game teal green jersey 11 jacksonville jaguars nike alternate</a> <a href="http://www.soknorealtor.com/cubs-22-jason-heyward-white-strip-home-cooperstown-stitched-mlb-jersey-nflw">cubs 22 jason heyward white strip home cooperstown stitched mlb jersey</a> <a href="http://www.donnaemendoza.com/flyknit-lunar-2-bianca-jungle-chaussuresa">flyknit lunar 2 bianca jungle</a> <a href="http://www.erneyapi.com/kvinners-nike-free-3.0-v5-gr%C3%B8nn-gr%C3%A5-butya">kvinners nike free 3.0 v5 gr酶nn gr氓</a>
    incommonblog http://www.incommonblog.com/

  2. avatar kidtokidboise

    <a href="http://www.bhcsboosters.com/nike-free-3.0-v4-hot-punch-pink-zebra-skob">nike free 3.0 v4 hot punch pink zebra</a> <a href="http://www.denisgoulart.com/philadelphia-76ers-knit-hate">philadelphia 76ers knit</a> <a href="http://www.donggeomdo.com/nike-lebron-12-low-hommes-orange-runninge">nike lebron 12 low hommes orange</a> <a href="http://www.ozoessaouira.com/mennns-asics-gel-quantum-360-rosa-bl%C3%A5-schuhea">mennns asics gel quantum 360 rosa bl氓</a> <a href="http://www.annomaplexel.com/nike-free-run-4.0-rot-and-gray-man-scarpeb">nike free run 4.0 rot and gray man</a> <a href="http://www.bringribben.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a>
    kidtokidboise http://www.kidtokidboise.com/

  3. avatar gotitvideova

    <a href="http://www.emunashipping.com/air-jordan-retro-8-gul-himmelen-bl%C3%A5-scarped">air jordan retro 8 gul himmelen bl氓</a> <a href="http://www.freefna.com/nike-free-5.0-gray-mennns-for-salg-obuvc">nike free 5.0 gray mennns for salg</a> <a href="http://www.affiliaternd.com/enfants-adidas-light-up-chaussures-marron-blanc-chaussuresg">enfants adidas light up chaussures marron blanc</a> <a href="http://www.bebekhikayesi.com/femmes-mizuno-wave-prophecy-gris-bleu-ciel-chaussuresa">femmes mizuno wave prophecy gris bleu ciel</a> <a href="http://www.bladespares.com/dam%C3%A4nner-nike-air-max-2015-rot-rosa-butya">dam盲nner nike air max 2015 rot rosa</a> <a href="http://www.cikotein.com/nike-free-3.0-v5-wei%C3%9F-gr%C3%BCn-trainers-for-verkauf-butyc">nike free 3.0 v5 wei脽 gr眉n trainers for verkauf</a>
    gotitvideova http://www.gotitvideova.com/

  4. avatar juniorensea

    <a href="http://www.benbageltours.com/nike-blazer-blanco-cuero-scarpee">nike blazer blanco cuero</a> <a href="http://www.blogdahelo.com/nike-air-jordan-2-sort-infrar%C3%B8d-butya">nike air jordan 2 sort infrar酶d</a> <a href="http://www.mindteez.com/nike-air-pegasus-92-wei%C3%9F-orange-productions-chaussuresf">nike air pegasus 92 wei脽 orange productions</a> <a href="http://www.suncreamclub.com/air-jordan-3-retro-svart-cemennt-gr%C3%A5-kit-butya">air jordan 3 retro svart cemennt gr氓 kit</a> <a href="http://www.sunqianwen.com/jordan-retro-3-noir-blanc-noir-schuheg">jordan retro 3 noir blanc noir</a> <a href="http://www.synexiemobile.com/schwarz-and-wei%C3%9F-long-sleeve-polo-shirt-poloz">schwarz and wei脽 long sleeve polo shirt</a>
    juniorensea http://www.juniorensea.com/

  5. avatar ipartybooths

    <a href="http://www.freizeittrend.com/nike-air-max-90-iridescent-noir-kurpesc">nike air max 90 iridescent noir</a> <a href="http://www.instanalo.com/ray-ban-cats-made-in-france-sunglassese">ray ban cats made in france</a> <a href="http://www.olafhuth.com/limited-nike-camo-youth-larry-ogunjobi-jersey-nfl-65-cleveland-browns-2018-salute-to-service-nflr">limited nike camo youth larry ogunjobi jersey nfl 65 cleveland browns 2018 salute to service</a> <a href="http://www.pecanfirewood.com/air-jordan-17-plus-for-sale-dallas-skoa">air jordan 17 plus for sale dallas</a> <a href="http://www.ezzolawaz.com/mens-air-jordan-horizon-off-court-shoes-sneakerf">mens air jordan horizon off court shoes</a> <a href="http://www.suchmeldungen.com/nike-air-presto-se-bl%C3%A5-gull-kurpesa">nike air presto se bl氓 gull</a>
    ipartybooths http://www.ipartybooths.com/

  6. avatar olgroupusainc

    <a href="http://www.reprebates.com/adidas-prougeator-gerrard-kurpese">adidas prougeator gerrard</a> <a href="http://www.royalwaxqueen.com/ralph-lauren-femminile-crew-neck-jumper-polou">ralph lauren femminile crew neck jumper</a> <a href="http://www.cliffsidesec.com/salomon-chaussures-rose-gris-scarpeb">salomon chaussures rose gris</a> <a href="http://www.deriebeeck.com/nfl-robert-griffin-iii-womens-game-white-jersey-3-baltimore-ravens-nike-road-nfls">nfl robert griffin iii womens game white jersey 3 baltimore ravens nike road</a> <a href="http://www.faithstarters.com/nike-roshe-run-hombres-zapatos-olympic-azul-flor-scarpeg">nike roshe run hombres zapatos olympic azul flor</a> <a href="http://www.fotoversorger.com/free-3.0-v5-gr%C3%A5-l%C3%B8b-sko-uk-obuvc">free 3.0 v5 gr氓 l酶b sko uk</a>
    olgroupusainc http://www.olgroupusainc.com/

  7. avatar culturalsq

    <a href="http://www.lauraundleon.com/a-buon-mercato-nike-air-max-tailwind-5-femminile-nero-runninge">a buon mercato nike air max tailwind 5 femminile nero</a> <a href="http://www.michaelphenow.com/nike-mercurial-zapatosu">nike mercurial</a> <a href="http://www.kguides.com/kobe-10-elite-gr%C3%BCn-dot-chaussuresa">kobe 10 elite gr眉n dot</a> <a href="http://www.nvusforex.com/chicago-cubs-10-ron-santo-1969-cream-jersey-nflf">chicago cubs 10 ron santo 1969 cream jersey</a> <a href="http://www.dinerwaresb.com/asics-gel-hyper-33-coffee-runningg">asics gel hyper 33 coffee</a> <a href="http://www.slipoverfx.com/women-2012-new-nfl-jerseys-san-francisco-49ers-53-navorro-bowman-red-jerseys-hall-of-fames-50th-patch-nflv">women 2012 new nfl jerseys san francisco 49ers 53 navorro bowman red jerseys hall of fames 50th patch</a>
    culturalsq http://www.culturalsq.com/

  8. avatar wethinkjerky

    <a href="http://www.kainjipark.com/indiana-university-baseball-hat-hata">indiana university baseball hat</a> <a href="http://www.lapthelakes.com/lebron-12-and-buckets-runninge">lebron 12 and buckets</a> <a href="http://www.delmaroffice.com/nike-huarache-high-blu-bianca-schuhea">nike huarache high blu bianca</a> <a href="http://www.medieneinsatz.com/hommes-nike-air-rejuven8-mule-3-bleu-scarpeb">hommes nike air rejuven8 mule 3 bleu</a> <a href="http://www.linexcanton.com/womens-nike-air-max-bw-white-netherlands-shoeso">womens nike air max bw white netherlands</a> <a href="http://www.saskostomy.com/nike-roshe-run-olive-gr%C3%B8nn-kurpesb">nike roshe run olive gr酶nn</a>
    wethinkjerky http://www.wethinkjerky.com/

  9. avatar qo8c01ke

    <a href="http://www.tvjuti65.com/nike-flyknit-lunar-1-marrone-arancia-kurpesb">nike flyknit lunar 1 marrone arancia</a> <a href="http://www.pbyz412s.com/adidas-nmd-runner-gris-mujeres-hack-schuhee">adidas nmd runner gris mujeres hack</a> <a href="http://www.x745mfcu.com/nike-lunarglide-7-femminile-bianca-gituttio-butyd">nike lunarglide 7 femminile bianca gituttio</a> <a href="http://www.dkouevy6.com/nike-flyknit-lunar-3-s%C3%B8lv-rosa-obuvc">nike flyknit lunar 3 s酶lv rosa</a> <a href="http://www.15wij42f.com/nike-kobe-11-flyknit-maschio-multicolor-scarpee">nike kobe 11 flyknit maschio multicolor</a> <a href="http://www.5k71d4ly.com/nike-air-max-plus-tn-marine-blau-gun-obuvd">nike air max plus tn marine blau gun</a>
    [url=http://www.qo8c01ke.com/]qo8c01ke[/url]

  10. avatar paypalwar

    <a href="http://www.wensemble.com/canada-goose-vest-mens-canadagooser">canada goose vest mens</a> <a href="http://www.offertborsen.com/burberry-scarf-etsy-womens-scarfc">burberry scarf etsy womens</a> <a href="http://www.checkmyvape.com/prada-hibiscus-backpack-2017-pradaf">prada hibiscus backpack 2017</a> <a href="http://www.pilaminates.com/t%C3%AAnis-adidas-springblade-drive-skor">t锚nis adidas springblade drive</a> <a href="http://www.tracithrasher.com/gianmarco-lorenzi-over-the-knee-boots-louboutinr">gianmarco lorenzi over the knee boots</a> <a href="http://www.visapuebla.com/mens-nike-air-max-2015-black-and-white-skor">mens nike air max 2015 black and white</a>

欢迎留言




用户登录

sitemap