Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Typedef function pointer?

I'm figuring out how to dynamically stack DLL's nevertheless what I don't know is this line
typedef void (*FunctionFunc)();

I have a couple of inquiries. On the off chance that somebody can answer them, I would be thankful.

For what reason is typedef utilized?

The syntax looks odd; after void ought to there not be a function name or something? It would seem that a mysterious function.

Is a function pointer made to store the memory address of a function?

So I'm befuddled right now; would you be able to explain things to me?
by

2 Answers

rahul07
1. typedef is used to alias types; in this case you're aliasing FunctionFunc to void()().

2. Indeed the syntax does look odd, have a look at this:

typedef void (*FunctionFunc) ( );
// ^ ^ ^
// return type type name arguments

No, this simply tells the compiler that the *FunctionFunc
type will be a function pointer, it doesn't define one, like this:

FunctionFunc x;
void doSomething() { printf("Hello there\n"); }
x = &doSomething;

x(); //prints "Hello there"
RoliMishra
1. Typedef is used to ease the reading of the code - especially for pointers to functions, or structure names.

2. That syntax is not obvious to read, at least when beginning. Using a typedef declaration instead eases the reading

3. Yes, a function pointer stores the address of a function. This has nothing to do with the typedef construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.

Example:

typedef int (t_somefunc)(int,int);

int product(int u, int v) {
return u*v;
}

t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123
456

Login / Signup to Answer the Question.