Function pointer without arguments types?

后端 未结 3 1032
鱼传尺愫
鱼传尺愫 2020-12-17 03:06

I was trying to declare a function pointer that points to any function that returns the same type. I omitted the arguments types in the pointer declaration to see what error

相关标签:
3条回答
  • 2020-12-17 03:52

    void (*pointer)() explains function pointed have unspecified number of argument. It is not similar to void (*pointer)(void). So later when you used two arguments that fits successfully according to definition.

    0 讨论(0)
  • 2020-12-17 03:59

    Empty parentheses within a type name means unspecified arguments. Note that this is an obsolescent feature.

    C11(ISO/IEC 9899:201x) §6.11.6 Function declarators

    The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

    0 讨论(0)
  • 2020-12-17 04:08

    There are couple of things you should know here...

    Function declaration:

    int myFunction();
    

    Function prototype:

    int myFunction(int a, float b);
    
    • A prototype is a special kind of declaration that describes the number and the types of function parameters.
    • A non-prototype function declaration doesn't say anything about its parameter.

    Example:

    int myFunction();
    

    This non-prototype function declaration does not mean that myFunction takes no arguments. It means that myFunction take an unspecified number of arguments. The compiler simply turns off argument type checking, number of arguments checking and conversion for myFunction.

    So you can do,

    int myFunction();   // The non-prototype signature will match a definition for
                        // myFunction with any parameter list.
    // This is the function definition... 
    int myFunction(int x, float b, double d)
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题