What is a function pointer in C? - Educative.io
文章推薦指數: 80 %
Just like an integer pointer or a char pointer, a function pointer is a variable that stores the address of a function. SolutionsEducativeEnterpriseEnablementplatformDevelopersLearnnewtechnologiesProductsCoursesforEnterpriseSuperchargeyourengineeringteamCoursesforIndividualsWorldclasscoursesOnboardingOnboardnewhiresfasterAssessmentsMeasureyourSkillScorePersonalizedLearningPlansPersonalizedPlansforyourgoalsProjectsBuildrealworldapplicationsAnswersTrustedAnswerstoDeveloperQuestionsPricingForEnterpriseTailoredforyourteamForIndividualsStayaheadofthecurveCoursesLogInJoinforfreeTrustedanswerstodeveloperquestionsTrustedAnswerstoDeveloperQuestionsRELATEDTAGSWhatisafunctionpointerinC?EducativeAnswersTeamJustlikeanintegerpointeroracharpointer,afunctionpointerisavariablethatstorestheaddressofafunction. Syntax Afunctionpointerisinitializedasfollows: void(*funcPtr)(int); Wecanbreakdowntheabovesyntaxintothefollowingcomponents: funcPtrisapointertoafunction. voidisthereturntypeofthatfunction. intisthedatatypeoftheargumentofthatfunction. Onceinitialized,youcanmakefuncPtrpointtoafunctionlikethis: funcPtr=&foo; fooisthenameofthefunctionwhichisbeingpointedto. Note:Theampersand(&)signisoptional.WritingfuncPtr=foo;isavalidsyntaxaswell. TheillustrationbelowshowshowfuncPtrpointstothefunctionfoo.ThefuncPtrvariablesimplystorestheaddressofthefunction. Functionpointers'point'tofunctionsbystoringtheirstartingaddressExamples Considertheexamplebelowwhichusesafunctionpointertoavoidfunction. Inlines6and7ofthecodesnippetbelow,thevariablefuncPtrpointstothefunctionshowMessage().Thefunctionisthencalled,usingthenameofthefunctionpointervariablefuncPtr. voidshowMessage(){ printf("Hellofromthefunction."); } intmain(){ void(*funcPtr)(); funcPtr=&showMessage; funcPtr(); }RunLet’shavealookatanotherexamplewhichutilizesafunctionpointerthattakesseveralarguments. Intheexamplebelow,themultipliertakesthreeintegersasarguments,thefunctionpointer,tothefunctionmultiplier,isinitializedwiththethreeintegersinline6. ThefunctionisinvokedusingthenameofthefunctionpointervariablefuncPtr. intmultiplier(intnum1,intnum2,intnum3){ returnnum1*num2*num3; } intmain(){ int(*funcPtr)(int,int,int); funcPtr=&multiplier; intresult=funcPtr(5,5,2); printf("%d",result); }RunRELATEDTAGSCopyright©2022Educative,Inc.AllrightsreservedRELATEDCOURSESViewallCoursesKeepExploringRelatedCoursesLearnin-demandtechskillsinhalfthetimeSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForEnterpriseForIndividualsRESOURCESEducativeBlogEducativeAnswersCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceABOUTUSOurTeamCareersHiringMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPricingForEnterpriseForIndividualsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServicePRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringRESOURCESEducativeBlogEducativeAnswersMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsRESOURCESEducativeBlogEducativeAnswersCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForEnterpriseForIndividualsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsCopyright©2022Educative,Inc.Allrightsreserved.
延伸文章資訊
- 1Functions Pointers in C Programming with Examples - Guru99
Pointers give greatly possibilities to 'C' functions which we are limited to return one value. Wi...
- 2What is the use of function pointers? [duplicate] - Stack Overflow
- 3Function pointer - Wikipedia
C++
- 4Function Pointer in C - Tutorialspoint
- 5Function Pointers in C and C++ - Cprogramming.com