Function that returns a function pointer

Started by
4 comments, last by ICanC 7 years, 1 month ago

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

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));
}

Thanks a lot.

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


#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

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

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

appreciate all the repsonses

This topic is closed to new replies.

Advertisement