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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:59:31+00:00 2026-05-15T06:59:31+00:00

That’s an odd title. I would greatly appreciate it if somebody could clarify what

  • 0

That’s an odd title. I would greatly appreciate it if somebody could clarify what exactly I’m asking because I’m not so sure myself.

I’m watching the Stanford videos on Programming Paradigms(that teacher is awesome) and I’m up to video five when he started doing this:

void *lSearch( void* key, void* base, int elemSize, int n, int (*cmpFn)(void*, void*))

Naturally, I thought to myself, “Oi, I didn’t know you could declare a function and define it later!”. So I created my own C++ test version.

int foo(int (*bar)(void*, void*));
int bar(void* a, void* b);

int main(int argc, char** argv)
{
    int *func = 0;
    foo(bar);

    cin.get();
    return 0;
}

int foo(int (*bar)(void*, void*))
{
    int c(10), d(15);
    int *a = &c;
    int *b = &d;
    bar(a, b);
    return 0;
}

int bar(void* a, void* b)
{
    cout << "Why hello there." << endl;
    return 0;
}

The question about the code is this: it fails if I declare function int *bar as a parameter of foo, but not int (*bar). Why!?

Also, the video confuses me in the fact that his lSearch definition

void* lSearch( /*params*/ , int (*cmpFn)(void*, void*)) is calling cmpFn in the definition, but when calling the lSearch function

lSearch( /*params*/, intCmp );

also calls the defined function int intCmp(void* elem1, void* elem2); and I don’t get how that works. Why, in lSearch, is the function called cmpFn, but defined as intCmp, which is of type int, not int* and still works? And why does the function in lSearch not have to have defined parameters?

  • 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-15T06:59:31+00:00Added an answer on May 15, 2026 at 6:59 am

    int (*cmpFn)(void*, void*) is a function pointer — a pointer to a function that can be called later. When you call iSearch you pass it a function that takes two void* and returns an int, and it binds that to the parameter cmpFn. Then iSearch can do something like int x = cmpFn(voidPtr1, voidPtr2); to call that function, passing it voidPtr1 and voidPtr2 as its arguments and storing the return value in x

    You can try a simple example by just declaring a function pointer and using it in the same function:

    int test1(int x) {return x;}
    int test2(int x) {return x+1;}
    
    int main(int argc, char** argv) {
        int (*fn)(int); // Function pointer named 'fn' that can hold a function that takes one int argument and returns an int
        int rtn;
    
        fn = test1; // Assign the 'test1' function to 'fn'
        rtn = fn(4); // Call 'fn' ('test1') with the argument 4; it returns 4 and stores it in 'rtn'
    
        fn = test2; // Assign the 'test2' function to 'fn'
        rtn = fn(4); // Call 'fn' ('test2') with the argument 4; it returns 5 and stores it in 'rtn'
    }
    

    It fails if you declare int *bar because that’s not a function pointer, it’s just a pointer to an integer. The syntax for function pointers is rtn_type (*name)(param1_type, param2_type, ...)

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

Sidebar

Related Questions

That title wasn't very descriptive; I wasn't sure how to shorten my question... Let's
That sounds familiar I'm sure, but it's not a CSS hack issue. I'm literally
That title is presumably awfully worded. I have some PostgreSQL tables. There is a
That's how I'm new in here on the website so it would be super
That one is a tricky one I believe. I would like to merge two
That's a really awkward title :) I need to write a report that generates
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's not entirely accurate. I have a user table and some/most Users are Trainers.
That is my table #EmployeeTemp . What I would like to do is retrieving
That is my select function: public function search($for) { $q = $this->select()->from($this->_name, array('id', 'title',

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.