2

I want to pass to a function a pointer that can point to one of several functions.

What is the syntax for this?

void func_a(char c){
    //
}
void func_b(char c){
    //
}

void receiver(void (*function_pointer)()
{
    // do stuff with the pointer to the function, e.g. call it:
    function_pointer('a');
    function_pointer('b');
    function_pointer('c');
}

void main(){
    receiver(&func_a); // calls func_a with 'a', then 'b', then 'c'
    receiver(&func_b); // calls func_b with 'a', then 'b', then 'c'
}

Will the above work as expected? I assume a function pointer can only be used for functions with the same signature?

2
  • 1
    Have you tried it? Did you get an error or are you just curious if this is correct? Commented Jun 17, 2016 at 13:31
  • I once had a similar problem with (optional addable) preprocessors on text files. you could solve it by having a struct with a functor, and a pointer to the next "step" in your function chain. you can then walk through the whole process by using something like while( processor) { processor.exec(); processor = processor.next ); Commented Jun 17, 2016 at 13:31

3 Answers 3

3

Yes, that looks like it should work.

And yes, you can only use the single pointer for functions sharing a signature.

Minor notes:

  • You don't need to use & to take the address of a function, the function's name evaluates to its address in suitable contexts.
  • Functions that are local and only intended to be used as callbacks (func_a() and func_b()) should be declared as static.
Sign up to request clarification or add additional context in comments.

Comments

2

One thing you can do to make this cleaner is use a typedef. Define your function pointer in a typedef and you can use it in the arguments. Also you don't need the &. Example with your code.

#include <stdio.h>

static void func_a(char c)
{
        printf("a:%c\n", c);
}

static void func_b(char c)
{
        printf("b:%c\n", c);
}

typedef void (*function)(char c);

void receiver( function func )
{
        func('a');
        func('b');
        func('c');
}

void main()
{
        receiver(func_a);
        receiver(func_b);
}

I learned this from 'learn C the hard way' link: http://c.learncodethehardway.org/book/ex18.html

Comments

1

Function pointer to select one function among multiple functions

#include <stdio.h>
int plus(int a,int b){return a+b;}
int minus(int a,int b){return a-b;}
int multiply(int a,int b){return a*b;}
int divide(int a,int b){return a/b;}
int percentage(int a,int b){return a%b;}
void gec(int(**p)(int ,int),int c)
{
c=c%5;
if(c==0)
*p=plus;
else if(c==1)
*p=minus;
else if(c==2)
*p=multiply;
else if(c==3)
*p=divide;
else
*p=percentage;
}
int main(void) {
int a=100,b=20,c=12,r;
int(*fptr)(int,int)=NULL;
gec(&fptr,c);
r=(*fptr)(a,b);
printf("%d",r);
return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.