Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function pointer without arguments types?

Tags:

c

pointers

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
like image 243
rullof Avatar asked Dec 30 '13 07:12

rullof


People also ask

What is meant by function without arguments?

Function with no argument and no return value: When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return a value, the calling function does not receive any data from the called function.

Can a pointer be used as a function argument?

6) Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function. For example, consider the following C program where wrapper() receives a void fun() as parameter and calls the passed function.

What is a pointer to function type in C++?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions.

What is function pointer with example?

Function Pointer Syntax void (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.


2 Answers

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.

like image 156
Yu Hao Avatar answered Oct 30 '22 04:10

Yu Hao


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.

like image 38
Dayal rai Avatar answered Oct 30 '22 04:10

Dayal rai