Typedefs for complex data types

后端 未结 2 1626
野性不改
野性不改 2021-02-10 03:39

I am attempting to understand the underlying mechanics of how C handles complex typedefs, from a syntax perspective.

Consider the following examples below (references in

2条回答
  •  深忆病人
    2021-02-10 04:02

    Simplfying KepaniHaole's rules a bit, it boils down to:

    1. Find the left-most identifer
    2. Work your way out, remembering that absent explicit grouping by parentheses, function-call () and [] bind before *.
    3. Apply recursively to any function parameters.

    Thus, T *a[] is an array of pointer tp T, T (*a)[] is a pointer to an array of T, T *f() is a function returning a pointer to T, , and T (*f)() is a pointer to a function returning T.

    Taking an example that gives a lot of people heartburn, we can look at the prototype for the POSIX signal function:

    void (*signal( int sig, void (*func)( int )))( int );
    

    which reads as

           signal                                          -- signal
           signal(                             )           -- is a function with
           signal(     sig                     )           -- parameter sig
           signal( int sig                     )           --   of type int
           signal( int sig,        func        )           -- and parameter func
           signal( int sig,      (*func)       )           --   of type pointer to
           signal( int sig,      (*func)(     ))           --   a function with
           signal( int sig,      (*func)( int ))           --     an int parameter
           signal( int sig, void (*func)( int ))           --   returning void
         (*signal( int sig, void (*func)( int )))          -- returning a pointer to
         (*signal( int sig, void (*func)( int )))(     )   --   a function with
         (*signal( int sig, void (*func)( int )))( int )   --     an int parameter
    void (*signal( int sig, void (*func)( int )))( int )   --   returning void
    

提交回复
热议问题