... I wonder the meaning of attribute definitions with dot (.) for struct attributes ...
It allows for you to access a specific element of the structure using the initialization syntax { }
. For example, consider this struct:
struct my_struct {
int field_1;
int field_2;
int field_3;
};
... it can be initialized as follows:
struct my_struct s1 = { 1, 2, 3 };
... or as follows:
struct my_struct s2 = { .field_1 = 1, .field_2 = 2, .field_3 = 3 };
... or if you do not know the order of the fields (or want to specify them in some order):
struct my_struct s3 = { .field_3 = 3, .field_1 = 1, .field_2 = 2 };
... remark that s1
is equivalent to s2
which is equivalent to s3
. Moreover, if you do not specify a field in your initialization then it will be zero. From the C99 standard 6.7.8.21:
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
... to illustrate this:
struct my_struct s4 = { .field_1 = 1 };
... that will zero-fill fields 2 and 3, hence s4.field_2 == 0
implies true
.