I'm having a hard time interpreting the following C code:
static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
const Netdev *netdev,
const char *name,
NetClientState *peer, Error **errp) = {
[NET_CLIENT_DRIVER_NIC] = net_init_nic,
#ifdef CONFIG_SLIRP
[NET_CLIENT_DRIVER_USER] = net_init_slirp,
#endif
[NET_CLIENT_DRIVER_TAP] = net_init_tap,
[NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
#ifdef CONFIG_VDE
[NET_CLIENT_DRIVER_VDE] = net_init_vde,
#endif
#ifdef CONFIG_NETMAP
[NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
#endif
#ifdef CONFIG_NET_BRIDGE
[NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
#endif
[NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
#ifdef CONFIG_VHOST_NET_USER
[NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
#endif
#ifdef CONFIG_L2TPV3
[NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
#endif
};
this looks like a function pointer declaration, but then it does = {...}
which I don't recognize. Is also does []
inside.
What this is exactly?
It might be easier to understand by using a type-alias:
// Define a type-alias for a pointer to a function
typedef int (*net_client_init_fun_type)(const Netdev *, const char *, NetClientState *, Error **);
// Define an array of pointers to functions
static net_client_init_fun_type net_client_init_fun[NET_CLIENT_DRIVER__MAX] = { ... }
It still requires knowledge about function-pointers and how they are declared/defined, but it will make the array declaration much easier to read.
The array initialization list uses designated array initialization .
When you have a line like:
[NET_CLIENT_DRIVER_NIC] = net_init_nic
in the array initialization list, it means that the index NET_CLIENT_DRIVER_NIC
of the array will be initialized to net_init_nic
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.