Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function pointer parameter without asterisk

Tags:

I have seen this definition of a function that receives a function pointer as parameter:

double fin_diff(double f(double), double x, double h  = 0.01) {   return (f(x+h)-f(x)) / h; } 

I am used to see this definition with an asterisk, i.e.:

double fin_diff(double (*f)(double), double x, double h  = 0.01); 

Do you know why the first definition is also valid?

like image 487
Freeman Avatar asked Jul 22 '19 10:07

Freeman


People also ask

How do you pass a pointer to a function as a parameter?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

Do you have to free function pointers?

You must not because free(ptr) is used only when pointer ptr is previously returned by any of malloc family functions. Passing free a pointer to any other object (like a variable or array element) causes undefined behaviour.

How is function pointer declared?

You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions. For z/OS® XL C/C++, use the __cdecl keyword to declare a pointer to a function as a C linkage.

What are the mandatory parts of function pointer?

What is the mandatory part to present in function pointers? Explanation: The data types are mandatory for declaring the variables in the function pointers.

What happens when you convert a function parameter to a pointer?

Note: If a function parameter is of a function type, it will be converted to a pointer to the function type. This means This only works for function parameters, not stand-alone function pointers, and so is of somewhat limited use.

What does the asterisk (*) mean when declaring multiple pointers?

When you declare multiple pointers in the same declaration, you write the asterisk ( *) together with the underlying type only. It isn't used as a prefix to each pointer name. For example:

What is a function pointer?

Function pointers are similar, except that instead of pointing to variables, they point to functions! Consider the following function: Identifier foo is the function’s name. But what type is the function? Functions have their own l-value function type -- in this case, a function type that returns an integer and takes no parameters.

How do you pass a function as a parameter in C?

In C programming you can only pass variables as parameter to function. You cannot pass function to another function as parameter. But, you can pass function reference to another function using function pointers. Using function pointer you can store reference of a function and can pass it to another function as normal pointer variable.


2 Answers

Standard says that these two functions are equivalent as function arguments are adjusted to be a pointer to function arguments:

16.1 Overloadable declarations [over.load]
(3.3) Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent. That is, the function type is adjusted to become a pointer to function type (11.3.5).

same in C:

6.7.5.3 Function declarators (including prototypes)
8 A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

like image 142
user7860670 Avatar answered Nov 11 '22 22:11

user7860670


Pointers to functions are peculiar. Given a function void f();, you can do

void (*fptr)() = f; void (*fptr)() = &f; void (*fptr)() = &&f; void (*fptr)() = &&&f; 

ad infinitum.

Similarly, when you call a function through a pointer to function you can do

fptr(); (*fptr)(); (**fptr)(); (***fptr)(); 

ad infinitum.

Everything collapses.

like image 33
Pete Becker Avatar answered Nov 11 '22 22:11

Pete Becker