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

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

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


前言

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

9 条评论

  1. avatar icallvoters

    <a href="http://www.lovemescort.com/georgia-tech-cap-hath">georgia tech cap</a> <a href="http://www.amardinlipi.com/adidas-tubular-runner-rouge-chaussuresh">adidas tubular runner rouge</a> <a href="http://www.penfoldscott.com/adidas-springblade-ignite-rouge-ul-chaussuresj">adidas springblade ignite rouge ul</a> <a href="http://www.periplaneto.com/nike-zoom-pegasus-31-blau-grau-schuhek">nike zoom pegasus 31 blau grau</a> <a href="http://www.kocaelimtm.com/anaheim-ducks-youth-jersey-nflh">anaheim ducks youth jersey</a> <a href="http://www.alanlgrimmdds.com/air-yeezy-2-menn-r%C3%B8d-svart-gr%C3%A5-obuvi">air yeezy 2 menn r酶d svart gr氓</a>
    icallvoters http://www.icallvoters.com/

  2. avatar lapaktoko

    <a href="http://www.laketreepress.com/chicago-bulls-d-wade-jersey-nflh">chicago bulls d wade jersey</a> <a href="http://www.prohaarklinik.com/coach-leather-wristlet-coachh">coach leather wristlet</a> <a href="http://www.bonfanticare.com/femminile-air-jordan-retro-8-rosso-nero-scarpeh">femminile air jordan retro 8 rosso nero</a> <a href="http://www.antoniopagni.com/femmes-nike-air-max-zero-jaune-rose-chaussuresp">femmes nike air max zero jaune rose</a> <a href="http://www.wcshosting.com/north-face-puffer-jacket-womens-sale-northfaceh">north face puffer jacket womens sale</a> <a href="http://www.biddingstream.com/nike-air-max-97-dam%C3%A4nner-wei%C3%9F-grau-schuheq">nike air max 97 dam盲nner wei脽 grau</a>
    lapaktoko http://www.lapaktoko.com/

  3. avatar ahmztradefx

    <a href="http://www.sguerradesign.com/femmes-nike-air-max-2017-violet-noir-chaussuresh">femmes nike air max 2017 violet noir</a> <a href="http://www.zeneventsja.com/nike-kobe-11-mujeres-amarillo-azul-zapatosp">nike kobe 11 mujeres amarillo azul</a> <a href="http://www.adharaweb.com/adidas-zx-850-purple-white-shoesi">adidas zx 850 purple white</a> <a href="http://www.jrelselaw.com/dam%C3%A4nner-nike-lebron-13-orangen-wei%C3%9F-schuhei">dam盲nner nike lebron 13 orangen wei脽</a> <a href="http://www.intellalegal.com/nike-kobe-7-verde-cielo-azul-zapatosk">nike kobe 7 verde cielo azul</a> <a href="http://www.carmelscoffee.com/herre-nike-flynit-air-max-sort-gul-skoi">herre nike flynit air max sort gul</a>
    ahmztradefx http://www.ahmztradefx.com/

  4. avatar fsscas

    <a href="http://www.theislandbody.com/kvinners-air-jordan-retro-10-bl%C3%A5-gull-obuvl">kvinners air jordan retro 10 bl氓 gull</a> <a href="http://www.zululeads.com/nike-air-max-black-white-flowers-shoesh">nike air max black white flowers</a> <a href="http://www.notmyrichie.com/nike-zoom-structure-18-arancia-grigio-scarpei">nike zoom structure 18 arancia grigio</a> <a href="http://www.mintyschoice.com/dam%C3%A4nner-nike-roshe-run-triangle-blau-orangen-schuhej">dam盲nner nike roshe run triangle blau orangen</a> <a href="http://www.mamapjblog.com/illinois-fighting-illini-top-of-the-world-ncaa-campus-cuff-knit-hath">illinois fighting illini top of the world ncaa campus cuff knit</a> <a href="http://www.pdfunicorn.com/nike-free-hypervenom-2-blanc-chaussuresh">nike free hypervenom 2 blanc</a>
    fsscas http://www.fsscas.com/

  5. avatar kudusbilinci

    <a href="http://www.adeohome.com/nike-air-max-thea-dam%C3%A4nner-himmelblau-lila-chaussuresa">nike air max thea dam盲nner himmelblau lila</a> <a href="http://www.kozmovip.com/femminile-air-jordan-5-cortuttio-foota">femminile air jordan 5 cortuttio</a> <a href="http://www.jorgevsells.com/kobe-9-low-influence-for-salg-runningd">kobe 9 low influence for salg</a> <a href="http://www.transitionbh.com/chicago-bulls-jersey-womens-nflh">chicago bulls jersey womens</a> <a href="http://www.bekbox.com/negro-or-blanco-converse-high-tops-zapatosh">negro or blanco converse high tops</a> <a href="http://www.larastart.com/nike-hyperdunk-2016-svart-himmelen-bl%C3%A5-scarpea">nike hyperdunk 2016 svart himmelen bl氓</a>
    kudusbilinci http://www.kudusbilinci.com/

  6. avatar frigoarreda

    <a href="http://www.randygageteam.com/nike-air-jordan-retro-11-bianca-scarpeh">nike air jordan retro 11 bianca</a> <a href="http://www.newlouisbag.com/adrian-peterson-reebok-jersey-nfli">adrian peterson reebok jersey</a> <a href="http://www.thebrandiac.com/nike-shox-r4-p%C3%BArpura-zapatosh">nike shox r4 p煤rpura</a> <a href="http://www.isisprojectsa.com/nike-lunarepic-low-flyknit-negro-zapatosk">nike lunarepic low flyknit negro</a> <a href="http://www.linnwoodpark.com/north-face-outer-shell-for-denali-fleece-northfaceh">north face outer shell for denali fleece</a> <a href="http://www.perfecthostia.com/adidas-damian-lillard-2-grau-light-gr%C3%BCn-schuhek">adidas damian lillard 2 grau light gr眉n</a>
    frigoarreda http://www.frigoarreda.com/

  7. avatar hiltonskyshop

    <a href="http://www.laketreepress.com/prada-fringe-backpack-pradah">prada fringe backpack</a> <a href="http://www.prohaarklinik.com/kam-chancellor-seattle-seahawks-jersey-nfli">kam chancellor seattle seahawks jersey</a> <a href="http://www.bonfanticare.com/kvinders-nike-zoom-structure-18-appelsin-gr%C3%A5-skol">kvinders nike zoom structure 18 appelsin gr氓</a> <a href="http://www.antoniopagni.com/long-sleeve-sports-polo-shirts-poloh">long sleeve sports polo shirts</a> <a href="http://www.wcshosting.com/mens-nike-roshe-run-hyp-pink-gold-shoesi">mens nike roshe run hyp pink gold</a> <a href="http://www.biddingstream.com/nike-free-tr-womens-black-green-shoesp">nike free tr womens black green</a>
    hiltonskyshop http://www.hiltonskyshop.com/

  8. avatar haysocial

    <a href="http://www.nordseepflege.net/adidas-zx-750-gr%C3%A5-sort-bl%C3%A5-schuhee">adidas zx 750 gr氓 sort bl氓</a> <a href="http://www.northsidecarservice.net/adidas-pr%C3%B8dator-gr%C3%B8nn-2014-butyd">adidas pr酶dator gr酶nn 2014</a> <a href="http://www.northstarcamp.net/nike-air-jordan-melo-m10-hvid-wolf-gr%C3%A5-obuvc">nike air jordan melo m10 hvid wolf gr氓</a> <a href="http://www.noviservice.net/air-jordan-7-retro-gris-and-violet-office-scarpee">air jordan 7 retro gris and violet office</a> <a href="http://www.nuovaideaviaggi.net/nike-kyrie-3-cool-gris-anthracite-polarized-azul-deck-obuvd">nike kyrie 3 cool gris anthracite polarized azul deck</a> <a href="http://www.objetpub.net/hommes-asics-gel-quantum-360-or-rose-chaussuresg">hommes asics gel quantum 360 or rose</a>
    [url=http://www.haysocial.net/]haysocial[/url]

  9. avatar kathimartuza

    <a href="http://www.kideschool.com/nike-blazer-low-id-schuh-scarpea">nike blazer low id schuh</a> <a href="http://www.killersandbox.com/air-jordan-true-flight-blanco-court-azul-apron-kurpesf">air jordan true flight blanco court azul apron</a> <a href="http://www.mymascensores.com/dan-hampton-womens-limited-olive-jersey-nike-nfl-chicago-bears-2017-salute-to-service-99-nflr">dan hampton womens limited olive jersey nike nfl chicago bears 2017 salute to service 99</a> <a href="http://www.paypalwar.com/nike-kobe-10-gr%C3%BCn-poison-frog-kurpesf">nike kobe 10 gr眉n poison frog</a> <a href="http://www.bamoments.com/adidas-nmd-r1-runner-primeknit-dark-bl%C3%A5-schuhef">adidas nmd r1 runner primeknit dark bl氓</a> <a href="http://www.fintecsales.com/nike-roshe-run-blum%C3%A4nner-grau-obuvf">nike roshe run blum盲nner grau</a>
    kathimartuza http://www.kathimartuza.com/

欢迎留言




用户登录

sitemap