0

I am very much interested in cracking minute things in C.

Function pointer:

From what i know, a function pointer is nothing more than a C variable which points the address of a function as a normal C variable. So that we can call the function using pointer also.

Questions:

  1. What is the necessity of using function pointers rather than using functions alone?
  2. Will it do any advanced thing which a normal function cannot do?
  • 3
    Search for and read about *callbacks*. – Some programmer dude Jun 01 '15 at 08:32
  • Hi Joachim Pileborg, thank you for introducing new word in programming. It looks callbacks helpfull in multithreading. How it will be usefull in embedded developement – Lokesh V Gowda Jun 01 '15 at 08:36
  • 2
    [qsort()](http://en.cppreference.com/w/c/algorithm/qsort), [atexit()](http://en.cppreference.com/w/c/program/atexit), [signal()](http://en.cppreference.com/w/c/program/signal)... – DevSolar Jun 01 '15 at 08:37

4 Answers4

1

According to Wikipedia, “In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.”

  1. In C callbacks are implemented using function pointers. For example, See this link.

  2. Can a normal function take another function as one of its arguments ? Callback is an advanced thing in that sense and function pointers implement them.

Further, another use case is explained here.

Community
  • 1
  • 1
Novak007
  • 426
  • 1
  • 9
  • 23
0

The difference and use cases are basically the same as pointers vs. regular objects. You can store a pointer to a function and configure at compile time/runtime what function would be called. Consider as the simplest example qsort:

void qsort( void *ptr, std::size_t count, std::size_t size,
            int (*comp)(const void *, const void *) );

There is a single qsort implementation, but you can order the same array of elements with different criteria.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

When you don't know, at compile time, what function to use, you can use function pointers to solve that problem.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef int (*fx)(int, int);
int one(int a, int b) { return a + b + 1; }
int two(int a, int b) { return a + b + 2; }
int three(int a, int b) { return a + b + 3; }
int four(int a, int b) { return a + b + 4; }

int main(void) {
    fx arfx[4] = {one, two, three, four};
    srand(time(0));
    for (int k = 0; k < 10; k++) {
        int n = rand() % 4; // 0, 1, 2, or 3
        int val = arfx[n](1, -1); // call one of functions in arfx
        printf("result is %d.\n", val);
    }
    return 0;
}

See the code running at ideone.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • Hi pmg, Sorry i didnt get how function pointers will help during compile time,? if i dont know which functon to use. I didnt undergone any such issue. Can you please elaborate it.? – Lokesh V Gowda Jun 01 '15 at 08:45
  • 1
    Rather than using an `if` construction to select what function to call, use an array of function pointers and directly select one with array indexing. – pmg Jun 01 '15 at 11:07
  • thank you Mr. pmg now its clear. Is there any improvement in execution speed or something else? if we call the functions using array of function pointers, rather than using if statement. – Lokesh V Gowda Jun 02 '15 at 02:36
  • If there is an improvement in execution speed it mostly is irrelevant. This array of function pointers is just another way to do something. With lots of functions, the array method is more succint and as easy to understand as a series of `if`s. – pmg Jun 02 '15 at 06:27
0

Questions:

What is the necessity of using function pointers rather than using functions alone?

A function pointer can point to various functions with the same signature, allowing one to create polymorphic structures. It can also act as callback - therefore the calling code need not know about the callable code (Callable code provides the function - Dependency Inversion Principle).

Will it do any advanced thing which a normal function cannot do?

You can provide a function pointer to as callback to a piece of code that needn't need the name of the function to perform the call. qsort mentioned elsewhere is a good example of this.

Werner Erasmus
  • 3,988
  • 17
  • 31