Thread: Tricky typedef

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    3

    Tricky typedef

    What is the purpose of this?

    typedef unsigned * (*item)(void* yyy);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cdecl: C gibberish <-> English
    It's a function pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    3
    What is the difference then between the a bove to the ordinary way?
    The ordinary way is: typedef void (Item_t)(int)
    What can be the usage of this?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you have some context for the question?

    > What can be the usage of this?
    Typedef'ing a function pointer is very common.

    Primarily because they're quite complicated to get right, and they get very verbose in a hurry.
    So summarising a whole bunch of ( ) and * into a single word makes good sense.


    Typedef'ing a function is basically unheard of.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Trying to explain the declaration... This:
    Code:
    unsigned int func( void *p );
    Is a function named func which returns an unsigned int and takes a pointer to void as argument, right?

    This:
    Code:
    unsigned int *func(void *p);
    Is the same, but returns a pointer to unsigned int. So, this other one is a pointer to a function which returns a pointer to unsigned int and takes a pointer to void as argument:
    Code:
    unsigned int *(*funcptr)(void *);
    Now we can transform this declaration in a typedef:
    Code:
    typedef unsigned int *(*funcptr_T)(void *);
    And use the type funcptr_T, instead of that complicated declaration, for example:
    Code:
    typedef unsigned int *(*funcptr_T)(void *);
    
    unsigned int *f( void *p ) { ... do something... }
    unsigned int *g( void *p ) { ... do something else ... }
    
    unsigned int *example( unsigned int n, void *arg )
    {
      // table with pointers to functions.
      const static funcptr_T fptr[] = { f, g };
    
      if ( n < 2 )
        return fptr[n](arg);  // indirect call through a pointer.
      return NULL; 
    }

  6. #6
    Registered User
    Join Date
    Feb 2021
    Posts
    3
    Thanks flp1969, this is helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c tricky code
    By theju112 in forum C Programming
    Replies: 6
    Last Post: 08-12-2011, 05:34 PM
  2. Tricky C
    By ronrardin in forum C Programming
    Replies: 2
    Last Post: 09-29-2010, 01:01 PM
  3. explain me this tricky qs
    By ppanda04 in forum C Programming
    Replies: 9
    Last Post: 09-24-2010, 08:19 AM
  4. tricky stuff...
    By dug in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2005, 08:23 AM
  5. Heres a tricky one
    By RoD in forum Tech Board
    Replies: 5
    Last Post: 08-16-2003, 10:07 AM

Tags for this Thread