3

I am a little bit confused about this subject. I know what callback is but function pointers are a little bit more confusing to me. Can they mean the same thing (at least in C/C++ context)? Or is one of them including the other, like function pointers can be used as a callback or vice versa.

Is there a relationship between function pointers and callbacks, or are they completely different subjects?

Can Bayar
  • 497
  • 4
  • 16
  • Are you familiar with the concept of pointers to begin with? – Michael Kenzel Apr 24 '19 at 20:49
  • have you used `qsort` from `stdlib.h`? – Mayur Apr 24 '19 at 20:50
  • 3
    A callback is a programming idiom, and a fairly general pattern for performing custom work within the context of some larger system whose details are typically hidden to you. A function pointer on the other hand is a concrete tool, and merely a way of referring to a function; you can use it for a callback, but you can use it for lots of other things too. – alter_igel Apr 24 '19 at 20:51
  • I am familiar with pointers, just never really needed the function pointers so far. I used sort functions too, I know how to pass a comparison function as a callback. – Can Bayar Apr 24 '19 at 20:53
  • A function pointer is just a variable that can hold a pointer to an arbitrary function. When you pass a callback to a function like `qsort`, the parameter variable is a function pointer. – Barmar Apr 24 '19 at 20:56
  • 1
    There is a good discussion here about _[function pointers](https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work)_. – ryyker Apr 24 '19 at 20:56
  • The distinction between function pointers and callbacks is clearer in c++ where callbacks are usually provided as template arguments or encapsulated in `std::function` objects. In c callbacks are usually function pointers so the distinction may be harder to see. – François Andrieux Apr 24 '19 at 21:08

1 Answers1

6

Function pointers are often used to implement callbacks. A classic example is the qsort function:

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

The 4th argument to this function is a function pointer. This will typically point to a function you wrote yourself that the qsort function will call. Here's an example of how it's used:

int cmp(const void *v1, const void *v2)
{
    const int *i1 = v1;    // C++ requires cast, C doesn't
    const int *i2 = v2;    // C++ requires cast, C doesn't
    if (*i1 < *i2) {
        return -1;
    } else if (*i1 > *i2) {
        return 1;
    } else {
        return 0;
    }
}

int main()
{
    int array[] = { 3, 7, 5, 1, 2, 9, 0, 4 };
    qsort(array, 8, sizeof(int), cmp);
}

In this example, cmp is used to tell the qsort function how the elements of the list are to be ordered.

Another example of function pointers that is not a callback is if you want to call a particular type of function based on some flag. For example:

void foo(int case_sensitive)
{
    int (*cmpfunc)(const char *, const char *);
    if (case_sensitive) { 
        cmpfunc = strcmp;
    } else {
        cmpfunc = strcasecmp;
    }

    ...
    // set strings str1 and str2
    ...
    if (cmpfunc(str1, str2) == 0)) {
        printf("strings are equal\n");
    } else {
        printf("strings are not equal\n");
    }
    ...
    // set strings str3 and str4
    ...
    if (cmpfunc(str3, str4) == 0)) {
        printf("strings are equal\n");
    } else {
        printf("strings are not equal\n");
    }
    ...
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    @ryyker It is not. Converting to/from a `void *` can be done freely without a cast. – dbush Apr 24 '19 at 21:00
  • So I can use a function pointer to pass a callback, but they also have other uses? Is this a correct inference? – Can Bayar Apr 24 '19 at 21:00
  • @dbush Converting *to* `void*` implicitly is allowed. Converting *from* `void*` requires a cast. Edit : In c++. I didn't notice this had both tags. – François Andrieux Apr 24 '19 at 21:03
  • Thank you, that clarifies it for me. I will keep the conversation open for a while in order to see if people give more feedbacks. :) – Can Bayar Apr 24 '19 at 21:10
  • I would go so far as to say that function pointers have few, if any, legitimate uses cases that cannot reasonably be cast in terms of callbacks. Callbacks are a design idiom. Function pointers are the C mechanism for implementing that idiom. – John Bollinger Apr 24 '19 at 21:27