2

How does the following code works without giving a name for the typedef in the 3rd line?How compiler assumes pf as a new data type?

#include <stdio.h>

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

int main(){
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}

int fun(int a, int b){
    return (a==b);
}

int proc(pf p, int a, int b){
    return ((*p)(a, b));
}
Praburaj
  • 613
  • 8
  • 21
  • pf is the name of the Pointer to the Function ;) – Morten Jensen Sep 01 '13 at 18:11
  • I think the question here is *what are the rules* for what typedef considers the alias. IOW, `typedef int int32` considers int32 the alias. What are the rules that in the function typedef above that the 2nd of 4 parameters is considered the alias? – Duck Sep 01 '13 at 18:20
  • if you write `typedef int* name` then `name` is the alias for the type (`int*`), in the case of a function pointer it just looks a bit more complicated but it is the same principle. – AndersK Sep 01 '13 at 18:53

5 Answers5

7

It does give it a name:

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

That's the typedef'd name. It reads as: pf is a pointer to a function that takes two ints and returns an int.

For more details on how function pointer typedefs work, see:

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
2

pf acts as an alias for a function which takes two ints as argument and returns an int.

It is a function pointer.

P0W
  • 46,614
  • 9
  • 72
  • 119
2

Syntactically, typedef is treated as storage class specifier, like extern or static. It doesn't actually specify a storage class; it's just defined that way for convenience. So you can replace typedef by static and get a declaration with a different meaning, but similar syntax, and defining the same identifier.

You have:

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

Replacing typedef by static gives:

static int (*pf) (int, int);

which declares a function pointer named pf. The typedef version of the declaration also declares the identifier pf, but as a type name, not as a pointer object.

Keep in mind that typedef doesn't create a new type, merely a new name for an existing type. Rather than declaring pf as an object, the typedef declaration makes pf an alias for the type name int (*)(int, int).

Since storage class specifiers are optional, you can also understand a complex typedef declaration by dropping the word typedef:

int (*pf) (int, int);

which declares pf as an object of type int (*) (int, int).

You can also use the cdecl program to explain complex declarations like this. It doesn't understand typedef (as of version 2.5), but you can just drop the typedef keyword:

$ cdecl
Type `help' or `?' for help
cdecl> explain typedef int (*pf) (int, int);
syntax error
cdecl> explain int (*pf) (int, int);
declare pf as pointer to function (int, int) returning int
cdecl> 

If you don't have the cdecl program installed on your system, there's an online version at http://cdecl.org/

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

pf as typedef is a type definition ; it defines the signature of a function (what to provide as arguments and what it will return). For the function proc using p as a variable; it defines the 'contract' when executing this function at address p ; this contract can only be checked at compile time, because basically at runtime the p variable is only an address.

Fx Archi
  • 13
  • 3
0

The typedef declaration has got a name -- 'pf'. And it represents a function pointer (instead of pointing to data, it points to executable code in memory).

When declaring function pointers, typedef does not follow the (usual) pattern -- "typedef oldTypeName newAliasName". Rather, the alias name (i.e. pf in above example) appears in between the return type (on left) and the argument types (on right).

Siddhartha Ghosh
  • 2,988
  • 5
  • 18
  • 25