How to cast function pointer?

Discussion in 'C Programming' started by Devrobcom, Sep 26, 2003.

  1. Devrobcom

    Devrobcom Guest

    Hi
    I know that this is not the Lint forum, but I get some Lint warnings
    when setting my pointers to NULL. And the problem is
    I don't know howto cast a function ptr the right way.

    typedef int FP(char*, char*);

    FP *pfp;
    pfp = NULL; --error 64: (Error -- Type mismatch (assignment) (ptrs to
    void/nonvoid))

    I have tried, without success:
    pft = (FP*)NULL;
    pfp = (int(*)(char*, char*))NULL;
     
    Devrobcom, Sep 26, 2003
    #1
    1. Advertisements

  2. Devrobcom

    Richard Bos Guest

    "Devrobcom" <> wrote:

    > I know that this is not the Lint forum, but I get some Lint warnings
    > when setting my pointers to NULL. And the problem is
    > I don't know howto cast a function ptr the right way.
    >
    > typedef int FP(char*, char*);
    >
    > FP *pfp;
    > pfp = NULL; --error 64: (Error -- Type mismatch (assignment) (ptrs to
    > void/nonvoid))


    That is an incorrect error. NULL is required to be a null pointer
    constant; therefore, when being assigned to _any_ type of pointer, it is
    required to result in a null pointer of the appropriate type; and null
    pointers can, without problem, be assigned to any kind of pointer, no
    matter whether they're object or function pointers. Lint is talking out
    of its hat. There is no more correct way to do this - what you've done
    is already quite correct.

    Richard
     
    Richard Bos, Sep 26, 2003
    #2
    1. Advertisements

  3. Devrobcom

    Derk Gwen Guest

    "Devrobcom" <> wrote:
    # Hi
    # I know that this is not the Lint forum, but I get some Lint warnings
    # when setting my pointers to NULL. And the problem is
    # I don't know howto cast a function ptr the right way.
    #
    # typedef int FP(char*, char*);
    #
    # FP *pfp;
    # pfp = NULL; --error 64: (Error -- Type mismatch (assignment) (ptrs to
    # void/nonvoid))
    #
    # I have tried, without success:
    # pft = (FP*)NULL;
    # pfp = (int(*)(char*, char*))NULL;

    I don't use NULL, I just use 0.

    cat <<':eof' >/tmp/t.c
    void f(void) {
    typedef int FP(char*, char*);
    FP *pfp;
    pfp = 0;
    }
    :eof
    cc -c /tmp/t.c


    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I hope it feels so good to be right. There's nothing more
    exhilirating pointing out the shortcomings of others, is there?
     
    Derk Gwen, Sep 26, 2003
    #3
    1. Advertisements

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. glen stark
    Replies:
    2
    Views:
    844
    Ron Natalie
    Oct 10, 2003
  2. tirath
    Replies:
    3
    Views:
    925
    Ivan Vecerina
    Oct 12, 2003
  3. Fraser Ross
    Replies:
    4
    Views:
    1,293
    Fraser Ross
    Aug 14, 2004
  4. murgan
    Replies:
    6
    Views:
    5,218
    Thad Smith
    Dec 21, 2005
  5. Vijai Kalyan
    Replies:
    4
    Views:
    1,054
    Vijai Kalyan
    Nov 8, 2005
  6. Szabolcs Borsanyi
    Replies:
    6
    Views:
    860
    Ben Bacarisse
    May 23, 2008
  7. Replies:
    1
    Views:
    452
  8. Hansen
    Replies:
    3
    Views:
    1,438
    rep_movsd
    Apr 24, 2010
Loading...