What is a function pointer in C? - Educative.io

文章推薦指數: 80 %
投票人數:10人

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.



請為這篇文章評分?