Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do function pointers work?

I'm asking some specific questions.

  1. How can I initialize them in a class?
  2. How can I pass a function as an argument?
  3. Do function pointers need to be declared and defined in the class?

For question number 2 here is what I mean:

void s(void) {
   //...
}

void f(function) { // what should I put as type to pass a function as an argument
   //...
}

f(s);
like image 496
Ramilol Avatar asked Dec 25 '10 00:12

Ramilol


People also ask

How do pointers to functions work?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

How does function pointers work in C?

1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.

What is function pointer explain with an example?

Function Pointer Syntax void (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.

What are function pointers and how are they useful?

A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory.


3 Answers

To define a function pointer, use the following syntax:

return_type (*ref_name) (type args, ...)

So, to define a function reference named "doSomething", which returns an int and takes in an int argument, you'd write this:

int (*doSomething)(int number);

You can then assign the reference to an actual function like this:

int someFunction(int argument) {
   printf("%i", argument);
}

doSomething = &someFunction;

Once that's done, you can then invoke it directly:

doSomething(5); //prints 5

Because function pointers are essentially just pointers, you can indeed use them as instance variables in your classes.

When accepting function pointers as arguments, I prefer to use a typedef instead of using the cluttered syntax in the function prototype:

typedef int (*FunctionAcceptingAndReturningInt)(int argument);

You can then use this newly defined type as the type of the argument for the function:

void invokeFunction(int func_argument, FunctionAcceptingAndReturningInt func) {
   int result = func(func_argument);
   printf("%i", result);
}

int timesFive(int arg) {
   return arg * 5;
}
invokeFunction(10, &timesFive); //prints 50
like image 192
Jacob Relkin Avatar answered Oct 22 '22 12:10

Jacob Relkin


it is not the strict answer , but to include configurable/assignable code as a class member, I would mention using a class/struct with the operator() . For example:

struct mycode
{
    int k;

    mycode(int k_) : k(k_)
    {
    }

    int operator()(int x)
    {
     return x*k;
    }
};


class Foo
{
public : Foo(int k) : f(k) {}
public : mycode f;
};

You can do:

Foo code(5);
std::cout << code.f(2) << std::endl;

it will print '10' , it I wrote everything ok.

like image 41
Javier Loureiro Avatar answered Oct 22 '22 12:10

Javier Loureiro


You have to declare f in this way:

void f(void (*x)())
{
  x();  // call the function that you pass as parameter (Ex. s()).
}

here is an excellent tutorial about function pointers and callbacks.

like image 42
Luis G. Costantini R. Avatar answered Oct 22 '22 13:10

Luis G. Costantini R.