14

Generally, we use typedef to get alternate names for datatypes. For example --

typedef long int li; // li can be used now in place of long int

But, what does the below typedef do?

typedef int (*pf) (int, int);
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
phoenix
  • 644
  • 10
  • 22
  • 2
    [cdecl](http://cdecl.org) to the rescue! – milleniumbug Jun 25 '13 at 03:29
  • 2
    possible duplicate of [Typedef function pointer?](http://stackoverflow.com/questions/4295432/typedef-function-pointer) – user93353 Jun 25 '13 at 03:30
  • 1
    @milleniumbug: Did you try it? Syntax error! – MatthewD Jun 25 '13 at 03:34
  • Its typedef function pointer but this question emphasizes easy understanding. since its more lucid. – phoenix Jun 25 '13 at 03:35
  • @milleniumbug: Unfortunately, cdecl simply says "syntax error" on this code. Perhaps you should check it yourself before suggesting something like this. :) – lnafziger Jun 25 '13 at 03:36
  • @lnafziger I think the real question is why OP finds the former to be "valid" but the latter to be weird. cdecl is still useful in the sense that knowing "... pf [is a] pointer to function (int, int) returning int" means that `typedef int (*pf) (int, int)` is just giving a shorthand name for a "function (int, int) returning int". – rliu Jun 25 '13 at 03:44
  • 2
    @roliu: If the OP doesn't know what this code is (and is therefore asking the question), do you really think that he is going to be able to make that translation and figure that out? – lnafziger Jun 25 '13 at 03:47
  • @lnafziger My point was that he doesn't need to figure anything out. He needs to get rid of the barrier that he's created mentally between an `int` and a function pointer. If he didn't consider them to be so different then perhaps he wouldn't have asked the question. I can imagine also having this question and entering in `int (*pf) (int, int)` into cdecl; at which point I might think "huh... I guess you... could just declare a function pointer `pf foo = ...`. Hm... Cool!" Or perhaps the real question is "how do you use functions as variables?" Basically my answer to your question is, "yes". – rliu Jun 25 '13 at 03:54
  • 2
    @roliu: Well, I think that he is asking this question because of a basic lack of understanding which makes it very difficult to "reason" things out. This site is for people of all skill levels, and something that is simple for you is not always simple for a novice programmer. His specific question was "what does the below typedef do" indicating that he is not familiar enough with the language to figure it out on his own and he needs help... So, my response to you is, "Not everyone is at your skill level, so please give him a break and just answer the question." :-) – lnafziger Jun 25 '13 at 03:59
  • @lnafziger I was just pointing out that cdecl could help OP here; that was literally the only point of my comment. I'm not sure where you construed that I thought he wasn't allowed to ask this question. I suggested a train of thought where cdecl would prove fruitful... that's it. – rliu Jun 25 '13 at 04:37

5 Answers5

28
typedef int (*pf) (int, int);

This means that variables declared with the pf type are pointers to a function which takes two int parameters and returns an int.

In other words, you can do something like this:

#include <stdio.h>

typedef int (*pf)(int,int);

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

int main(void) {
    pf xyzzy = addUp;
    printf ("%d\n", xyzzy (19, 23));
    return 0;
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
5
typedef long int li;

assigns alternate name li to type long int.

In exactly the same way

typedef int (*pf) (int, int);

assigns alternate name pf to type int (*) (int, int). That all there is to it.

As you probably noticed, typedef declarations follow the same syntax as, say, variable declarations. The only difference is that the new variable name is replaced by the new type name. So, in accordance with C declaration syntax, the declared name might appear "in the middle" of the declarator, when array or function types are involved.

For another example

typedef int A[10];

declares A as alternate name for type int [10]. In this example the new name also appears "in the middle" of the declaration.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

It's a function pointer prototype. You can then declare a function as an argument something like this:

void RegisterCallback(pf your_callback_func);

Then you can can call the function passed as a func ptr:

...
your_callback_func(i, j);
...
spartygw
  • 3,289
  • 2
  • 27
  • 51
0

The typedef has the name pf and it is for a function pointer that takes two integers as arguments and returns an integer.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

typedef works as:

Define unknown type with known types.

So it defines function type that takes two int argument and return int.

mattn
  • 7,571
  • 30
  • 54