1

So I'm practicing pointers to functions, and tried out making this simple program, here's a snippet of it. It still gives me an error "invalid lvalue" when it comes to assigning the address. funcptr = &addnum for example. Also I can't help but wonder what's the use of this? Isn't it much simpler to call the function? Or am I misunderstanding something

#include <stdio.h>
int arithnum(int base);
int addnum(int base,int new);
int subnum(int base,int new);
int mulnum(int base,int new);
int divnum(int base,int new);
typedef int *ptrdef(int,int);
int arithnum(int base)
{
    char operator;
    int operand;
    ptrdef funcptr;
    printf("Enter operator: ");
    scanf("\n%c",&operator);
    printf("Enter second operand: ");
    scanf("%d",&operand);
    switch(operator)
    {
        case '+':
            funcptr = &addnum;
            break;
        case '-':
            funcptr = &subnum;
            break;
        case '*':
            funcptr = &mulnum;
            break;
        case '/':
            funcptr = &divnum;
            break;
    }
    return funcptr(base,operand);
}
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
  • 2
    [What are function pointers used for...](http://stackoverflow.com/questions/1758564/what-are-function-pointers-used-for-and-how-would-i-use-them). – DCoder Sep 12 '12 at 07:28

2 Answers2

1

Change your typdef.

Change:

typedef int *ptrdef(int,int);

to

typedef int (*ptrdef)(int,int);

To answer your other question/statement: "Function pointers seem useless": In your example their use is trivial, but a more useful example is vtables in C++. Function pointers allow a base class to define the signature of a function and then subclasses can replace those function pointers with their own implementation changing how the object responds to a function.

You can also use them in a COM model API where the main application dynamically links with a plugin and they plugin returns structure of function pointers for the requested interface.

James
  • 1,341
  • 7
  • 13
  • I'm pretty sure you have to, well tutorials in the internet say so. And K&R – jantristanmilan Sep 12 '12 at 07:20
  • It is not useful to change the thing on the right side if you have an invalid lvalue. In this case, something with `funcptr` is wrong. See my answer. – glglgl Sep 12 '12 at 07:21
  • You do not have to take the address or deference the funcptr either. `funcptr = divnum` and `funcptr(base,operand)` compile just fine. – James Sep 12 '12 at 07:31
1

ITYM

typedef int (*ptrdef)(int,int);

as your version is a function which returns an int * while you want a function pointer which returns an int.


Just a hint: I know that the following is not common sense, but I prefer to typedef the function itself and then do

typedef int myfunc(int,int);
myfunc therealfunction; // bites me if I do a mistake
int therealfunction(int a, int b)
{
    // do stuff and
    return 42;
}
myfunc * funcptr = &therealfunction;

in order to get bitten by an error instead of a warning if I accidentally change the declaration of therealfunction.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Thanks man!! I got it now, but how about my 2nd question? What's the use of this? Its much more simpler to call the function.. – jantristanmilan Sep 12 '12 at 07:27
  • The use is, commonly spoken, to decide what to do at one time and really do it later. Doing so, you can e. g. create a job queue system, or a table of function in an array, mapping an index (essentially an int) to a function to be executed. – glglgl Sep 12 '12 at 08:23