Pointer Indirection Syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Matthews

    #1

    Pointer Indirection Syntax

    Hi,

    At my work, we have a need for a pointer to a pointer
    to a function returning void and having void parameters.

    Since this is an embedded system, we have to assign the
    variable with a constant value (address).

    We came up with the following:
    #define LOCATION 0x10000
    typedef void (*FuncPtr)(void );
    typedef *FuncPtr PtrFuncPtr;

    /* usage */
    if (*((PtrFuncPtr) LOCATION) != NULL)
    {
    (**LOCATION)(); /* Execute function */
    }

    The above "if" statement should be translated to:
    If the value at 0x10000 is not NULL,
    execute the function pointed to by the
    value in location 0x10000.
    The function takes void parameters and returns void.

    What is the syntax for declaring a pointer to a pointer
    to a function taking no parameters and returning void?
    {In other words, what is the syntax to combine the
    two typedefs into one?}
    {Or /where does the 2nd asterisk (*) get placed?}

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

  • Joona I Palaste

    #2
    Re: Pointer Indirection Syntax

    Thomas Matthews <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > scribbled the following:[color=blue]
    > Hi,[/color]
    [color=blue]
    > At my work, we have a need for a pointer to a pointer
    > to a function returning void and having void parameters.[/color]
    [color=blue]
    > Since this is an embedded system, we have to assign the
    > variable with a constant value (address).[/color]
    [color=blue]
    > We came up with the following:
    > #define LOCATION 0x10000
    > typedef void (*FuncPtr)(void );
    > typedef *FuncPtr PtrFuncPtr;[/color]
    [color=blue]
    > /* usage */
    > if (*((PtrFuncPtr) LOCATION) != NULL)
    > {
    > (**LOCATION)(); /* Execute function */
    > }[/color]
    [color=blue]
    > The above "if" statement should be translated to:
    > If the value at 0x10000 is not NULL,
    > execute the function pointed to by the
    > value in location 0x10000.
    > The function takes void parameters and returns void.[/color]
    [color=blue]
    > What is the syntax for declaring a pointer to a pointer
    > to a function taking no parameters and returning void?
    > {In other words, what is the syntax to combine the
    > two typedefs into one?}
    > {Or /where does the 2nd asterisk (*) get placed?}[/color]

    Let me try this by hand. I might screw up, so any C gurus out there,
    feel free to correct me.
    Function taking no parameters and returning void:
    void function(void)
    Pointer to a function taking no parameters and returning void:
    void (*function)(voi d)
    Pointer to a pointer to a function taking no parameters and returning
    void:
    void (**function)(vo id)
    I believe that's it.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The obvious mathematical breakthrough would be development of an easy way to
    factor large prime numbers."
    - Bill Gates

    Comment

    • Eric Amick

      #3
      Re: Pointer Indirection Syntax

      On 7 Apr 2004 19:00:51 GMT, Joona I Palaste <palaste@cc.hel sinki.fi>
      wrote:
      [color=blue]
      >Thomas Matthews <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > scribbled the following:[color=green]
      >> Hi,[/color]
      >[color=green]
      >> At my work, we have a need for a pointer to a pointer
      >> to a function returning void and having void parameters.[/color]
      >[color=green]
      >> Since this is an embedded system, we have to assign the
      >> variable with a constant value (address).[/color]
      >[color=green]
      >> We came up with the following:
      >> #define LOCATION 0x10000
      >> typedef void (*FuncPtr)(void );
      >> typedef *FuncPtr PtrFuncPtr;
      >> What is the syntax for declaring a pointer to a pointer
      >> to a function taking no parameters and returning void?
      >> {In other words, what is the syntax to combine the
      >> two typedefs into one?}
      >> {Or /where does the 2nd asterisk (*) get placed?}[/color]
      >
      >Let me try this by hand. I might screw up, so any C gurus out there,
      >feel free to correct me.
      >Function taking no parameters and returning void:
      >void function(void)
      >Pointer to a function taking no parameters and returning void:
      >void (*function)(voi d)
      >Pointer to a pointer to a function taking no parameters and returning
      >void:
      >void (**function)(vo id)
      >I believe that's it.[/color]

      That's fine, and the original typedefs were *almost* correct. Note the
      change in the second:

      typedef void (*FuncPtr)(void );
      typedef FuncPtr *PtrFuncPtr;

      In general, to create a typedef, declare a variable of the appropriate
      type, then stick "typedef" in front.

      --
      Eric Amick
      Columbia, MD

      Comment

      Working...