Reputation: 95
I recently came across this while browsing the Linux kernel source:
struct netlink_kernel_cfg cfg = {
.input = scsi_nl_rcv_msg,
.groups = SCSI_NL_GRP_CNT,
};
The full source is here.
I am trying to understand how the member definitions .input
and .groups
work? The C struct
s I am familiar with are similar to:
struct employee{
char name[30];
int empId;
float salary;
};
So how do .input
and .groups
work?
Upvotes: 2
Views: 267
Reputation: 2881
As mosvy said in a comment, these are C99 designated initialisers.
struct netlink_kernel_cfg cfg = {
.input = scsi_nl_rcv_msg,
.groups = SCSI_NL_GRP_CNT,
};
doesn’t declare a structure (as your employee
example does), it declares a variable, cfg
, of type struct netlink_kernel_cfg
, and initialises two of its members, input
and groups
.
The main advantages of this syntax are that the declaration order no longer matters, that members can be omitted (and are initialised in the same way as static variables), and that it’s easier to read.
Upvotes: 3