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. Advertising

  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. Advertising

  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. 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. tirath
    Replies:
    3
    Views:
    717
    Ivan Vecerina
    Oct 12, 2003
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,096
    Dan Pop
    Feb 10, 2004
  3. Szabolcs Borsanyi
    Replies:
    6
    Views:
    671
    Ben Bacarisse
    May 23, 2008
  4. Replies:
    1
    Views:
    344
  5. Hansen
    Replies:
    3
    Views:
    1,120
    rep_movsd
    Apr 24, 2010
Loading...

Share This Page