Jump to content
  • Advertisement
Sign in to follow this  
ICanC

Function that returns a function pointer

This topic is 559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, I'm trying to make a calculator and I'm struggling to make a function invoke the right function based on a function pointer (for practice)

I'm struggling to understand how to define a function pointer, and the syntax on how it should be implemented. Please see below the attempt I've made so far :

#include <stdio.h>

int add(int, int);
int sub(int, int);
int mul(int, int);
int (*getfunc())(char);

int main(void)
{
	puts("\t\tCalculator\n\n");
	
	return 0;
}

int add(int a, int b)
{
	return a + b;
}

int sub(int a, int b)
{
	return a - b;
}

int mul(int a, int b)
{
	return a * b;
}

int (*getfunc())(char op)
{
	switch(op)
	{
		case '+':  return getfunc = add;
		case '-':  return getfunc = sub;
	}
}

however the compiler never likes the syntax for the function implementation, can someone please point me in the right direction? Thanks

Edited by ICanC

Share this post


Link to post
Share on other sites
Advertisement

The easier way to define the type correctly is to first create a typedef for your function type and then define your function returning that type. getfunc can't set his own value in its implementation. And it can't be a function pointer if you are trying to implement it as a function getting a char and returning a function pointer. getfunc would also have a different type than add or sub..

typedef int (*op_func)(int, int);

// .. define add and sub

op_func getfunc(char op)
{
    switch(op) {
        case '+': return add;
        case '-': return sub;
    }
}

int main()
{
    fprintf("5 + 7 = %d", getfunc('+')(5, 7));
    fprintf("5 - 7 = %d", getfunc('-')(5, 7));
}
Edited by apatriarca

Share this post


Link to post
Share on other sites

Thanks a lot.

 

Just for understanding, can you advise what the code would look like without using typedef?

Share this post


Link to post
Share on other sites
#include <stdio.h>
#include <stdlib.h>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }

typedef int (*FNP)(int, int);

FNP get_func(int op)
{
    if (op == '+') return &add;
    return &sub;
}

int main(void)
{
/* 2 equivalent ways to write 'fnc', a point to an "int f(int, int)" function */
/*    FNP fnc; */
    int (*fnc)(int, int);

    int a = 3;
    int b = 5;
    int result;

    fnc = get_func('+');

    result = (*fnc)(a, b);
    printf("%d + %d = %d\n", a, b, result);
    return 0;
}

Thanks a lot.

 

Just for understanding, can you advise what the code would look like without using typedef?

http://stackoverflow.com/questions/997821/how-to-make-a-function-return-a-pointer-to-a-function-c

 

but I couldn't make it work.

 

Anyway, it looks too weird to even try without typedef

Share this post


Link to post
Share on other sites

Thanks a lot.
 
Just for understanding, can you advise what the code would look like without using typedef?


Something like this:
#include <stdio.h>

int add(int x, int y) {
  return x + y;
}

int subtract(int x, int y) {
  return x - y;
}

int (*get_function(char c))(int,int) {
  return (c == '+') ? add : subtract;
}

int main() {
  printf("%d\n", get_function('+')(5,7));
}


Here's a useful link: http://stackoverflow.com/questions/10758811/c-syntax-for-functions-returning-function-pointers

Share this post


Link to post
Share on other sites

Thanks Alvaro, that code works and the link is good too

 

appreciate all the repsonses

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!