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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:55:18+00:00 2026-05-31T20:55:18+00:00

I am trying to understand function pointers and am stuggling. I have seen the

  • 0

I am trying to understand function pointers and am stuggling. I have seen the sorting example in K&R and a few other similar examples. My main problem is with what the computer is actually doing. I created a very simple program to try to see the basics. Please see the following:

#include <stdio.h>

int func0(int*,int*);
int func1(int*,int*);

int main(){

    int i = 1;
    myfunc(34,23,(int(*)(void*,void*))(i==1?func0:func1));//34 and 23 are arbitrary inputs



}

void myfunc(int x, int y, int(*somefunc)(void *, void *)){

    int *xx =&x;
    int *yy=&y;

    printf("%i",somefunc(xx,yy));


}

int func0(int *x, int *y){

    return (*x)*(*y);

}

int func1(int *x, int *y){

    return *x+*y;
} 

The program either multiplies or adds two numbers depending on some variable (i in the main function – should probably be an argument in the main). fun0 multiplies two ints and func1 adds them.

I know that this example is simple but how is passing a function pointer preferrable to putting a conditional inside the function myfunc?
i.e. in myfunc have the following:

if(i == 1)printf("%i",func0(xx,yy));

else printf("%i",func1(xx,yy));

If I did this the result would be the same but without the use of function pointers.

  • 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-31T20:55:19+00:00Added an answer on May 31, 2026 at 8:55 pm

    Your understanding of how function pointers work is just fine. What you’re not seeing is how a software system will benefit from using function pointers. They become important when working with components that are not aware of the others.

    qsort() is a good example. qsort will let you sort any array and is not actually aware of what makes up the array. So if you have an array of structs, or more likely pointers to structs, you would have to provide a function that could compare the structs.

    struct foo {
      char * name;
      int magnitude;
      int something;
    };
    
    int cmp_foo(const void *_p1, const void *_p2)
    {
      p1 = (struct foo*)_p1;
      p2 = (struct foo*)_p2;
    
      return p1->magnitude - p2->magnitude;
    }
    
    struct foo ** foos;
    // init 10 foo structures...
    
    qsort(foos, 10, sizeof(foo *), cmp_foo);
    

    Then the foos array will be sorted based on the magnitude field.

    As you can see, this allows you to use qsort for any type — you only have to provide the comparison function.

    Another common usage of function pointers are callbacks, for example in GUI programming. If you want a function to be called when a button is clicked, you would provide a function pointer to the GUI library when setting up the button.

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

Sidebar

Related Questions

I have been trying to understand function pointers in C++ so that I can
I was trying to understand the difference between closures and function pointers, and I
I am trying to understand what the following typedefs mean. Are they function pointers?
I am trying to understand the concept of currying and calling a function which
I'm trying to understand why $('#title').replaceWith('ha'); will work outside the drop: function(event, ui) {}
This matrix transposition function works, but I'm trying to understand its step by step
I am trying to understand this example code regarding Browser Helper Objects. Inside, the
I am trying to understand how to use functional pointers to map the method
Trying to understand the behaviour of pointers in C, I was a little surprised
I was trying to understand something with pointers, so I wrote this code: #include

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.