资源描述:
《C语言指针函数和函数指针1》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C语言指针函数和函数指针1Istheaddressofanelementofanarray.Theoutputisthevalueinthisaddress・Two,functionpointerThepointertothefunctioncontainstheaddressofthefunction,andyoucancallthefunctionthroughit.Thedeclarationformatisasfollows:Thetypespecifier(*functionname)(parameters)Infact,thiscan
2、notbecalledafunctionname・Itshouldbecalledthevariablenameofthepointer・Thisspecialpointerpointstoafunctionthatreturnstheintegervalue・Thedeclarationandcuttingitpointstothefunctiondeclarationconsistent.Theparenthesesoutsidethepointerandpointeroperatorschangethedefaultoperatorprec
3、edence・Ifthereisnoparentheses,itbecomesaprototypedeclarationofthefunctionthatreturnstheintegerpointer.Suchas:Void(*fptr)();Assigningtheaddressofafunctiontoafunctionpointercantakethefollowingtwoforms:Fptr=&Function;Fptr二Function;Theaddressoperatorisnotrequiredbecauseonlyafunct
4、ionidentifierrepresentsitsaddress,andifitisafunctioncall,itmustincludeaparametertableenclosedinparentheses・Thefollowingtwowayscanbeusedtocallfunctionsthroughpointers:X二(*fptr)();X=fptr();Thesecondformatslooklikefunctioncalls・However,someprogrammersprefertousethefirstformatbec
5、auseitexplicitlystatesthatthefunctioniscalledthroughpointersratherthanfunctionnames・Here'sanexample:Void(*funcp)();Void,FileFunc(),EditFunc();(main)Funcp二FileFunc;(*funcp)();Funcp=EditFunc;(*funcp)();Void,FileFunc()Printf(〃F订eFunc\〃);Void,EditFunc()Printf(zzEditFunc\z
6、z);Programoutputas:FileFuncEditFuncThree,pointerThepointerofthepointerseemspuzzling.Theirdeclarationshavetwostars・Suchas:Char**cp;Ifyouhavethreestars,that'sthepointertothepointer・Thefourasteriskisthepointertothepointerofthepointer,andsoon.Whenyouarefamiliarwithsimpleexamples,
7、youcandealwithcomplexsituations・Ofcourse,inpractice,youuseonlytwopointers,andthreestarsareuncommon,letalonefourstars・Thepointerofapointerrequirestheaddressofthepointer.Charc二'A';Char*p=&c;Char**cp二&p;Apointertoapointercannotonlyaccessthepointeritispointingto,butalsoaccessth
8、edataitrefersto.Hereareafewexamples:Char*pl二*cp;Charcl=**cp;Youmight