Newbie Ques: ((void(*) (void *) 0)

Discussion in 'C Programming' started by Desmond Foley, Jun 7, 2005.

  1. Hi

    I have the following statement

    #define STATIC ((void(*) (void *) 0)


    The (void *) I understand as meaning a pointer to a void, so

    (void *) 0

    would be a void pointer to zero

    the meaning of void(*) is stopping me.

    I'd appreciate a translation.


    Regards

    Des
    Desmond Foley, Jun 7, 2005
    #1
    1. Advertising

  2. Desmond Foley

    Zoran Cutura Guest

    Desmond Foley <> wrote:
    > Hi
    >
    > I have the following statement
    >
    > #define STATIC ((void(*) (void *) 0)


    I think you're missing a closing paran here. This would probably be

    #define STATIC ( ( void (* ) (void *)) 0 )
    >
    >
    > The (void *) I understand as meaning a pointer to a void, so
    >
    > (void *) 0
    >
    > would be a void pointer to zero
    >
    > the meaning of void(*) is stopping me.
    >
    > I'd appreciate a translation.


    actually in this case the (void *) is a functions parameter list
    declaration.

    The void (*) () part tells us that it should be a pointer to a function
    returning nothing (void).

    So we have 0 casted to a pointer to function expecting a void-pointer
    and returning void.

    HTH, HAND

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
    Zoran Cutura, Jun 7, 2005
    #2
    1. Advertising

  3. On Tue, 07 Jun 2005 13:09:48 +0100, Desmond Foley wrote:

    > Hi
    >
    > I have the following statement
    >
    > #define STATIC ((void(*) (void *) 0)


    Note the parentheses don't match here, I'll assume you mean

    #define STATIC ((void(*) (void *)) 0)

    A cast contains a type which is similar to a declaration without the
    variable name. Consider where the variable name would go in a
    corresponding declaration:

    void(*var) (void *);

    Why is it there? There simply isn't anywhere else it could go. This
    declares var as a pointer to a function taking a void * argument and
    returning void. I've preserved the spacing which turns out to be a bit
    confusing, this would be better written as:

    void (*var)(void *);

    so your original #define might be better as

    #define STATIC ((void (*)(void *)) 0)

    > The (void *) I understand as meaning a pointer to a void, so
    >
    > (void *) 0
    >
    > would be a void pointer to zero
    >
    > the meaning of void(*) is stopping me.


    The spacing makes it look like that but it isn't. Remembering the
    declaration of var, the cast is to a pointer to a function taking a void *
    argument and returning void. So the expansion of STATIC evaluates to a
    null pointer of this type.

    Note that as a separate issue (void (*))p is a valid cast, however the
    inner parentheses are redundant and it is equivalent to (void *)p.

    Lawrence
    Lawrence Kirby, Jun 7, 2005
    #3
  4. You've got mismatched parentheses in that macro, but typically if you
    see (*) then it's part of a cast to a pointer to T where T would be a
    function or array.
    James Daughtry, Jun 7, 2005
    #4
  5. Desmond Foley

    c_learner Guest

    Desmond Foley wrote:
    > Hi
    >
    > I have the following statement
    >
    > #define STATIC ((void(*) (void *) 0)
    >
    >
    > The (void *) I understand as meaning a pointer to a void, so
    >
    > (void *) 0
    >
    > would be a void pointer to zero
    >
    > the meaning of void(*) is stopping me.
    >
    > I'd appreciate a translation.
    >
    >
    > Regards
    >
    > Des


    http://www.is.pku.edu.cn/~qzy/c/reading/pitfall.htm

    usually, that's kind of form is used for hardware addressing.
    c_learner, Jun 7, 2005
    #5
  6. Re: Newbie Ques: ((void(*) (void *)) 0)

    Hi

    Thanks Zoran and Lawerence.


    Yes I omitted a ')'


    it should have been

    #define STATIC ((void(*) (void *)) 0)



    I now understand that


    (void(*) (void *))


    is a cast, and the cast is to a pointer to a function. That function has
    one argument, a pointer to a void, the function returns void.


    I think I can imagine how that applies if we were casting a function,
    but I am unclear as to what it means for a number to be casted into a
    function.


    I am guessing now that the cast is to allow the zero (STATIC) to be
    accepted as an argument to a function, that expects a pointer to a
    function that has a single arg, a void pointer and returns a void.


    Regards
    Desmond Foley, Jun 7, 2005
    #6
  7. Desmond Foley

    Richard Bos Guest

    Re: Newbie Ques: ((void(*) (void *)) 0)

    Desmond Foley <> wrote:

    > I now understand that
    >
    > (void(*) (void *))
    >
    > is a cast, and the cast is to a pointer to a function. That function has
    > one argument, a pointer to a void, the function returns void.
    >
    > I think I can imagine how that applies if we were casting a function,
    > but I am unclear as to what it means for a number to be casted into a
    > function.


    For any other number, it would mean nothing whatsoever.

    However, the number in question was a literal 0. That's a null pointer
    constant. Null pointer constants can be cast to function pointer type -
    and the result is a null pointer of function pointer type. IOW, you get
    a function pointer that explicitly points nowhere, just as (int *)0 is
    an int pointer that points nowhere.

    Richard
    Richard Bos, Jun 7, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apple
    Replies:
    7
    Views:
    360
    Mike Wahler
    Sep 28, 2004
  2. Ollej Reemt
    Replies:
    7
    Views:
    499
    Jack Klein
    Apr 22, 2005
  3. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    775
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    392
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page