Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6390685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:34:41+00:00 2026-05-25T03:34:41+00:00

Just recently I have been asked in an interview: Where can we use function

  • 0

Just recently I have been asked in an interview: Where can we use function pointers in C?
And what function pointer returns. I said using we can call functions using function pointer then he asked some example but I could not satisfy with him some more example. He then asked me what function pointer return. I told him it depends upon the declaration of function pointers.

But I really would like to know some uses of function pointer in C.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:34:41+00:00Added an answer on May 25, 2026 at 3:34 am

    I think the classical example in C is qsort. Quoting from there (and I DO know it’s http://www.cplusplus.com, so a not-very-good site, but it seems to be correct)

    void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
    

    Function that compares two elements. The function shall follow this prototype:

    int comparator ( const void * elem1, const void * elem2 ); 
    

    The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.

    The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.


    The other “classical” example is the calculator (for example see this, it’s C++ but it’s the same in C).

    You have four math functions, for example

    float Plus    (float a, float b) { return a+b; }
    float Minus   (float a, float b) { return a-b; }
    float Multiply(float a, float b) { return a*b; }
    float Divide  (float a, float b) { return a/b; }
    

    in some way you select your operation

    /* Here there should be an if or a switch/case that selects the right operation */
    float (*ptrFunc)(float, float) = Plus;
    

    and you can call it later (for some reason you don’t want to call it directly in the if/switch, perhaps because you want to make other “things” that are “common” to all the operations, like logging or printing the result)

    float result = ptrFunc(1.0f, 2.0f);
    

    Another two things you can use function pointers for are callbacks (as written by vine'th) and “poor man” virtual functions (you put them in a struct and when you create the struct (you know, like using a “poor man” constructor, but this is C so we will call it an initializer), you save there what functions will be used by that struct).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started C very recently and I have been asked to answer
I've recently began using dTrace and have noticed just how awesome it is. Its
I have recently been introduced to EasyMock and have been asked to develop some
In my job project I have recently been asked to generate POM files via
Recently I have asked a question about what I should use to create self-contained
I have recently figured out that I haven't been using regex properly in my
I have recently been using cookie store and I want to transition to active
I am just getting started with Silverlight and have recently added a Silverlight project
I've recently decided that I just have to finally learn C/C++, and there is
Have had to write my first proper multithreaded coded recently, and realised just how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.