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 8505093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:13:18+00:00 2026-06-11T02:13:18+00:00

I feel like I have a pretty good handle on pointers in C. Mostly,

  • 0

I feel like I have a pretty good handle on pointers in C. Mostly, I use
them to pass arrays to functions.

But I’ve noticed in looking at many different code examples that it’ not uncommon to
see pointers to functions. It’s not at all clear to me why this would be useful?
Are there some classic instances where the pointer to function model is handy or essential to implement?

  • 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-06-11T02:13:19+00:00Added an answer on June 11, 2026 at 2:13 am

    The classic example is sorting an array of elements using the qsort function. Its last argument is a function pointer to the routine that compares two elements, passed as void pointers.

    void qsort(void *base,
               size_t nmemb,
               size_t size,
               int(*compar)(const void *, const void *));
    

    Using a function pointer here is essential since the sorting routine can’t be made generic enough to know how you want to compare the elements, so the function pointer is essentially a “callback” that the sort function uses to let you extend it.

    [Edit] For example, suppose you had the following structure defined:

    struct president {
        char * fname;
        char * lname;
        unsigned int number;
    };
    

    If you wanted to sort an array of them by their “number” field you could implement a “compar” (comparator) function as follows:

    int compare_presidents(const void * v1, const void * v2) {
        struct president p1 = * ((struct president *) v1);
        struct president p2 = * ((struct president *) v2);
        return (p1.number - p2.number); // Compare by number ascending.
    }
    

    And you could sort an array of such structures as such:

    qsort(presidents, num_presidents, sizeof(struct president), compare_presidents);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty new to Haskell but I feel like I have a decent
So I feel like im pretty close, but I also have a feeling I
I feel like this must have been asked before, but I've looked at Knockout
I feel like GUID/UUID questions have been done to death here, but I can't
I feel like I'm missing something pretty basic, but I can't seem to figure
I use PHP/mySQL/CodeIgniter pretty heavily, writing sql statements to handle/manipulate data. I feel doing
I feel like I have enough experience with CI to finally start fooling around
I feel like I have everything I need in my routes.rb, my controller, and
I feel like this is quite delicate, I have various folders whith projects I
I feel like I've entered the Twilight Zone. I have a Grails form that

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.