Re: typedef function pointer

From: Keith Thompson (kst-u_at_mib.org)
Date: 09/13/04


Date: Mon, 13 Sep 2004 17:16:13 GMT

Cancerbero <sgurin@montevideo.com.uy> writes:
> Hi (first, excuse me for my bad english)
>
> As I know, the semantics for typedef is:
>
> typedef A B;
>
> I think this makes B a synonym of A, where A is an existing data type. Is
> that right?

That's the syntax, not the semantics. "Syntax" refers to the way a
construct is written in C source; "semantics" refers to what it means.
(And actually that's not the syntax of a typedef, except in the
simplest cases.)

As Jens points out, it's like saying that the syntax of an object
declaration is
    A v;
where v is the object name and A is the type. But if you wnat v
to be an array of 20 ints, the declaration is
    int v[20];
which doesn't fit the simple "A v;" syntax.

But yes, "typedef A B;" makes B a synonym of A. That's the semantics.

> Based on the previous definition of typedef, I can't understand the next:
>
> typedef int (*TypeFunc) (int, int);

Syntactically, "typedef" is treated as a storage-class specifier, like
"extern", "static", "auto", or "register". It really isn't one (it
doesn't specify any kind of storage class), but it turned out to be
convenient to define the syntax that way (though one could argue that
it's not really all that convenient after all). So if you can
understand that

    extern int (*Typefunc) (int, int);

declares an external pointer object called "Typefunc", that points
to a function taking to int arguments and returning it, then you can
understand that

    typedef int (*Typefunc) (int, int);

declares a type name called "Typefunc", that refers to a pointer to a
function taking to int arguments and returning it.

Now it's just a matter of understanding C's declaration syntax.
Unfortunately, that's a non-trivial task.

The basic principle is that the declaration of an entity mirrors the
use of that entity. For example:
    int *a;
declares a as a pointer to int. You can also think of it as declaring
that "*a" is an int.

Dropping the extern/typedef and changing the name, consider:
    int (*foo) (int, int);
Back off a bit and look at the declaration as a whole. It says that
        (*foo) (int, int)
will yield an int value. It looks like a function call, so the stuff
before the arguments
               (int, int)
has to denote a function, so
        (*foo)
denotes a function, so
          foo
has to be a pointer to a function. We've already seen that the
function takes two int arguments, and that it returns int, so we can
conclude that
    int (*foo) (int, int);
declares foo as a pointer object, specifically as a pointer to a
function taking two int arguments and returning int.

Add "extern", and it declares foo as an extern object of the same type.

Add "typedef" instead of "extern", and it declares foo as a type name
for the same type.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.


Relevant Pages

  • Re: Porting from visual C to gcc problem
    ... the compiler must have come across the typedef ... >>function declaration, ... then the scope lasts to through the body to ... >>int func; ...
    (comp.lang.c.moderated)
  • Re: typedef with function pointers
    ... Forget about the typedef for a moment, ... you say "*x is an int, hence x is a pointer to int." ... the other elements of the declaration depends on the way ... and function pointers got answered. ...
    (comp.lang.c)
  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)
  • Re: disabling parse error in gcc
    ... the problem is with headers and nowhere else.Suppose a int has ... typedef struct MYSTRUCT{ ... putting the declaration of D_TYPE in header1.h, ...
    (comp.unix.programmer)
  • Re: incorrect printf o/p for doubles
    ... >> Undeclared variables don't default to int. ... >> been possible to omit the declaration altogether. ... Many pre-ANSI C compilers supported a syntax along the lines of: ...
    (comp.lang.c)