6

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 will be generated. But the program was compiled successfully and executed without any issue.

Is this a correct declaration? Shouldn't we specify the arguments types?

#include <stdio.h>
#include <stdlib.h>

void add(int a, int b)
{
    printf("a + b = %d", a + b);
}

void (*pointer)() = &add;

int main()
{
    add(5, 5);
    return 0;
}

Output:

a + b = 10
4
  • 2
    This is perfectly legal K&R C. Commented Dec 30, 2013 at 7:53
  • 1
    Sample code does not make sense as a sample. Commented Dec 30, 2013 at 8:03
  • @BLUEPIXY Why is that? Commented Dec 30, 2013 at 8:04
  • 1
    pointer has not been used in an assignment other than. By not know do not use even an invalid pointer even if it? It has not been the reason why the correct output is obtained at least. Commented Dec 30, 2013 at 8:07

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What does it mean by declarators?
@rullof Something about declaration(opposed to definition), I really don't how to explain it exactly.
7

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.

Comments

0

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)
{
    ...
}

1 Comment

float becomes double after promotion. You cannot use non-prototype declarations if you use non-fully-promoted argument types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.