I came across definition of a struct
that looked something like this in the linux kernel sources (net/ipv4/fib_trie.c
)-
struct key_vector {
t_key key;
unsigned char pos; /* 2log(KEYLENGTH) bits needed */
unsigned char bits; /* 2log(KEYLENGTH) bits needed */
unsigned char slen;
union {
/* This list pointer if valid if (pos | bits) == 0 (LEAF) */
struct hlist_head leaf;
/* This array is valid if (pos | bits) > 0 (TNODE) */
struct key_vector __rcu *tnode[0];
};
};
What does above definition of tnode
mean? I wrote a sample code to understand and print that looks like -
struct s {
union {
int i;
int *pi[0];
};
};
int main()
{
struct s s1;
s1.i = 0x12345678;
printf("sizeof(s1): %lu, s1.i: %x, s1.pi: %p, s1.*pi: %p\n", sizeof(s1), s1.i, s1.pi[0], s1.pi);
}
The output of which is -
sizeof(s1): 8, s1.i: 12345678, s1.pi: 0x7ffc12345678, s1.*pi: 0x7ffc2824add0
I am not entirely sure I understand this.