Thread: Compiler warning and a confusing function declaration.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Compiler warning and a confusing function declaration.

    OK basically I have a (probably harmless) compiler warning that, for the sake of completeness, I would like to remove if possible (go easy on me please, I'm a newbie). The key bits are (I think) ...

    The warning.
    Code:
    warning C4550: expression evaluates to a function which is missing an argument list
    askfor_aux and askfor_aux_keypress functions (and keypress_h function?) declared in a .h include file.

    Code:
    extern bool askfor_aux_keypress(char *buf, size_t buflen, size_t *curs, size_t *len, char keypress, bool firsttime);
    extern bool askfor_aux(char *buf, size_t len, bool keypress_h(char *, size_t, size_t *, size_t *, char, bool));
    askfor_aux_keypress function defined.

    Function defined, and assignment made of function pointer.
    Code:
    bool askfor_aux_keypress(char *buf, size_t buflen, size_t *curs, size_t *len, char keypress, bool firsttime)
    {
    ... skip rest ...
    askfor_aux function defined

    Code:
    bool askfor_aux(char *buf, size_t len, bool keypress_h(char *, size_t, size_t *, size_t *, char, bool))
    {
    ... skip bits ...
    		keypress_h = askfor_aux_keypress;
    Now the things I don't quite know are
    1) What's happening with that "declare a function inside a function declaration" thing that seems to be going on with keypress_h?
    And
    2) The intermediate keypress_h function pointer seems to be used to make it easy to switch out askfor_aux_keypress with a different function to handle collecting keypresses. However there are no alternative functions so I guess I can just ditch that bit and use askfor_aux_keypress directly?
    Oh, and like I asked at the top, 3) Why the warning, and can I get rid of it? (Other than just suppressing C4550 )

    [EDIT]Coming to think of it the fourth thing I don't get is why the rather similar looking assignments like ...
    Code:
    ang_sort_comp = ang_sort_comp_hook_longs;
    don't prompt the same compiler warning.

    Code:
    extern bool (*ang_sort_comp)(const void *u, const void *v, int a, int b);
    Code:
    static bool ang_sort_comp_hook_longs(const void *u, const void *v, int a, int b)
    {
    ...
    Last edited by PaulBlay; 05-20-2009 at 10:07 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're looking to declare a pointer to a function, you're missing some syntax.
    Code:
    bool askfor_aux(char *buf, size_t len, bool (*keypress_h)(char *, size_t, size_t *, size_t *, char, bool))
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by PaulBlay View Post
    Coming to think of it the fourth thing I don't get is why the rather similar looking assignments like
    Up to my depth on this one but I would say that a similar looking assignment is not necessarily a similar assignment. If "ang_sort_comp" is a function pointer, this is okay. If it is supposed to be a return value, it's a bad call.

    If the keypress_h function ptr is always the same you could take it right out of the parameter list and alter the function code appropriately, but on the other hand, this is a nifty dynamic thing that has already been coded, so you might as well leave it that way, even if it seems unlikely to be used much right now.
    Last edited by MK27; 05-20-2009 at 10:18 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Quote Originally Posted by Salem View Post
    If you're looking to declare a pointer to a function, you're missing some syntax.
    Brilliant. Compiler warning gone, program still works. :-)

Popular pages Recent additions subscribe to a feed