Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function pointer with named arguments?

I recently came across a strange syntax in C program.

struct connector_agent_api{
    bool (*receive)(slot *s, uint8_t *data, uint8_t length);
}

Is "receive" a function pointer?

If it is a function pointer, why does it have named arguments? Should it be like the following one?

bool (*receive)(slot *, uint8_t *, uint8_t);

It certainly compiled and being used in a library. I searched on internet a lot and tried to justify this kind of syntax. I still don't know why this thing can be compiled... :(

like image 891
ZuckerReis Avatar asked Apr 26 '19 16:04

ZuckerReis


People also ask

How do you pass a function pointer as an argument?

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.

Can a pointer be used as a function argument?

Pointer as a function parameter is used to hold addresses of arguments passed during function call. This is also known as call by reference. When a function is called by reference any change made to the reference variable will effect the original variable.

How do you use pointers as arguments in a function explain with a program?

This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments. If any change made by the function using pointers, then it will also reflect the changes at the address of the passed variable.

Is function name a function pointer?

Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address. For example, in the below program, we have removed address operator '&' in assignment.


Video Answer


2 Answers

The names of arguments in a function pointer are optional, just as the names of arguments in a function declaration are optional. This is because parameter names if given are not used, so both formats are allowed.

In section 6.7.6.3 of the C standard regarding Function Declarators, which includes both function prototypes and function pointers, paragraph 6 states:

A parameter type list specifies the types of, and may declare identifiers for, the parameters of the function.

The only place where function parameters require a name is in the actual definition of a function.

For a function definition, Section 6.9.1p5 states:

If the declarator includes a parameter type list, the declaration of each parameter shall include an identifier, except for the special case of a parameter list consisting of a single parameter of type void , in which case there shall not be an identifier. No declaration list shall follow.

like image 70
dbush Avatar answered Oct 17 '22 23:10

dbush


What makes you think it is a strange syntax? It is a valid declaration as per C standard. The fact that the parameters are named is irrelevant. The naming of such parameters is optional in this case. It can be really helpful if you or someone else is using an IDE because it could display the complete prototype upon using the function pointer to call the function and thus give a hint to the coder about the arguments to be supplied.

like image 43
machine_1 Avatar answered Oct 17 '22 23:10

machine_1