int pointer to two struct pointers?

908 views c
4

This is from the linux build files <linux/usb/serial.h>:

int (*calc_num_ports)(struct usb_serial *serial, struct usb_serial_endpoints *epds);

I'm just having a little trouble understanding what's going on here. An int pointer to two struct pointers? How does that work?

answered question

A pointer to a function taking two struct pointers and returning int. Learn how C declarations work.

1 Answer

1

It tells you what it is and what it's used for in the header (<linux/usb/serial.h>):

@calc_num_ports: pointer to a function to determine how many ports this
 *  device has dynamically. It can also be used to verify the number of
 *  endpoints or to modify the port-endpoint mapping. It will be called
 *  after the probe() callback is called, but before attach().

The syntax for a function pointer is not very intuitive. Consider the following:

int *calc_num_ports(struct usb_serial *serial, struct usb_serial_endpoints *epds);
int (*calc_num_ports)(struct usb_serial *serial, struct usb_serial_endpoints *epds);

The first is the prototype of a function named calc_num_ports that takes two arguments, struct usb_serial * and struct usb_serial_endpoints *, and returns an int *.

The second is a variable type that requires the address of a function that takes two arguments, struct usb_serial * and struct usb_serial_endpoints * and returns an int.

posted this

Have an answer?

JD

Please login first before posting an answer.