pointers to function problem

Discussion in 'C Programming' started by Mantorok Redgormor, Apr 5, 2004.

  1. I have ran into a problem where I have a struct
    that has a member which contains a pointer
    to function and is initialized to a function
    in the initializer list. With my array of
    structs of this type, I have some elements
    of this array, thats function pointer member does
    not need to be initialized to a function.
    I can't simply initialize it to NULL
    and I'm not sure if casting NULL(which can
    be 0 or (void *)0) to the function pointer type
    will work.

    If NULL is (void *)0 on some implementations
    you would end up with:

    (int (*)(unsigned int))(void *)0

    is something like that even guaranteed
    to work?

    I'm not even sure if just plain 0 and casting
    it to the function pointer type would work.
    Since function pointers might have different
    representations than object pointers, it seems
    you would end up with implementation-defined
    behavior either way.

    Has anyone ran into this problem before?
    Where portability was a concern to you
    and you couldn't rely on implementation-
    defined behavior.



    --
    nethlek
    Mantorok Redgormor, Apr 5, 2004
    #1
    1. Advertising

  2. In article <>,
    (Mantorok Redgormor) wrote:

    > I have ran into a problem where I have a struct
    > that has a member which contains a pointer
    > to function and is initialized to a function
    > in the initializer list. With my array of
    > structs of this type, I have some elements
    > of this array, thats function pointer member does
    > not need to be initialized to a function.
    > I can't simply initialize it to NULL
    > and I'm not sure if casting NULL(which can
    > be 0 or (void *)0) to the function pointer type
    > will work.
    >
    > If NULL is (void *)0 on some implementations
    > you would end up with:
    >
    > (int (*)(unsigned int))(void *)0
    >
    > is something like that even guaranteed
    > to work?


    NULL is a null pointer constant. Any null pointer constant can be cast
    to any pointer type and the result will be a null pointer of the
    corresponding type. That is true both for data pointers and function
    pointers. It is guaranteed to work.

    > I'm not even sure if just plain 0 and casting
    > it to the function pointer type would work.


    A plain zero is also a null pointer constant and can be cast to a
    function pointer, producing a null pointer of the right type.

    > Since function pointers might have different
    > representations than object pointers, it seems
    > you would end up with implementation-defined
    > behavior either way.


    No, it is defined by the C Standard.
    Christian Bau, Apr 5, 2004
    #2
    1. Advertising

  3. Mantorok Redgormor wrote:
    > I have ran into a problem where I have a struct that has a member
    > which contains a pointer to function and is initialized to a
    > function in the initializer list. With my array of structs of this
    > type, I have some elements of this array, thats function pointer
    > member does not need to be initialized to a function. I can't
    > simply initialize it to NULL and I'm not sure if casting NULL(which
    > can be 0 or (void *)0) to the function pointer type will work.


    Just initialize it to an uncast null pointer constant (e.g. NULL or
    0).

    #include <math.h>
    #include <stddef.h>

    struct {
    double (*one)(double),
    (*two)(double);
    } functions[] = { {sin, NULL}, {NULL, cos} };

    Null pointer constants are special in C: assigning a null pointer
    constant to a function pointer type is allowed, and has the desired
    behaviour.

    > If NULL is (void *)0 on some implementations
    > you would end up with:
    >
    > (int (*)(unsigned int))(void *)0
    >
    > is something like that even guaranteed
    > to work?


    It's guaranteed to work

    > I'm not even sure if just plain 0 and casting
    > it to the function pointer type would work.


    There's no need to cast at all.

    > Since function pointers might have different
    > representations than object pointers, it seems
    > you would end up with implementation-defined
    > behavior either way.


    Casts deal with values, not representations.

    Jeremy.
    Jeremy Yallop, Apr 5, 2004
    #3
  4. Christian Bau <> wrote in message news:<>...
    > In article <>,
    > (Mantorok Redgormor) wrote:
    >
    > > I have ran into a problem where I have a struct
    > > that has a member which contains a pointer
    > > to function and is initialized to a function
    > > in the initializer list. With my array of
    > > structs of this type, I have some elements
    > > of this array, thats function pointer member does
    > > not need to be initialized to a function.
    > > I can't simply initialize it to NULL
    > > and I'm not sure if casting NULL(which can
    > > be 0 or (void *)0) to the function pointer type
    > > will work.
    > >
    > > If NULL is (void *)0 on some implementations
    > > you would end up with:
    > >
    > > (int (*)(unsigned int))(void *)0
    > >
    > > is something like that even guaranteed
    > > to work?

    >
    > NULL is a null pointer constant. Any null pointer constant can be cast
    > to any pointer type and the result will be a null pointer of the
    > corresponding type. That is true both for data pointers and function
    > pointers. It is guaranteed to work.
    >
    > > I'm not even sure if just plain 0 and casting
    > > it to the function pointer type would work.

    >
    > A plain zero is also a null pointer constant and can be cast to a
    > function pointer, producing a null pointer of the right type.
    >
    > > Since function pointers might have different
    > > representations than object pointers, it seems
    > > you would end up with implementation-defined
    > > behavior either way.

    >
    > No, it is defined by the C Standard.


    what section?



    --
    nethlek
    Mantorok Redgormor, Apr 5, 2004
    #4
  5. Mantorok Redgormor

    Leor Zolman Guest

    On 5 Apr 2004 06:18:31 -0700, (Mantorok Redgormor) wrote:


    >>
    >> A plain zero is also a null pointer constant and can be cast to a
    >> function pointer, producing a null pointer of the right type.
    >>
    >> > Since function pointers might have different
    >> > representations than object pointers, it seems
    >> > you would end up with implementation-defined
    >> > behavior either way.

    >>
    >> No, it is defined by the C Standard.

    >
    >what section?


    6.3.2.3/3 and 4:

    3 An integer constant expression with the value 0, or such an expression
    cast to type void *, is called a null pointer constant.55) If a null
    pointer constant is converted to a pointer type, the resulting pointer,
    called a null pointer, is guaranteed to compare unequal to a pointer to any
    object or function.

    4 Conversion of a null pointer to another pointer type yields a null
    pointer of that type. Any two null pointers shall compare equal.

    -leor

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Apr 5, 2004
    #5
    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. Phil
    Replies:
    1
    Views:
    644
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    760
    Ron Natalie
    Sep 18, 2003
  3. Peter Goddard

    void pointers & void function pointers

    Peter Goddard, May 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    514
    Peter Goddard
    May 16, 2005
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    468
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
Loading...

Share This Page