Definition of a strange structure

In the Intel DPDK sample code, I found this strange syntax construct. Can someone explain to me what this means?

static const struct rte_eth_conf port_conf = {
    .rxmode = {
        .split_hdr_size = 0,
        .header_split   = 0,
        .hw_ip_checksum = 0,
        .hw_vlan_filter = 0,
        .jumbo_frame    = 0,
        .hw_strip_crc   = 0,
    },
    .txmode = {
    }
};
+3
source share
3 answers

If you

struct X
{
    type_a var_a;
    type_b var_b;
    type_c var_c;
    type_d var_d;
};

you can initialize the object as follows:

struct X x = {value_a, value_b, value_c, value_d};

But this means that you need to know the order of the variables in X, and also have an initial value for all of this. Alternatively, you can initialize as follows:

struct X x = {
    .var_a = value_a,
    .var_b = value_b,
    .var_c = value_c,
    .var_d = value_d
};

That way, you can initialize member variables in any order, or even skip some.

, , , . , .

+2

C99, .

C , . . , , , .

. :

int a[6] = { [4] = 29, [2] = 15 };
+6

This is a C99 function called designated initializers . It allows you to specify the names of the fields on which you set the values, rather than specify the values ​​in the order in which the corresponding fields appear in the ad. In addition, this syntax allows you to initialize alliance members other than the first, which was not possible before C99.

+2
source

All Articles