C structure question

L

linux.lover

Hello all,
I am analysing 2.4 and 2.6 series ip_output.c file code. What i
found is that 2.6 kernel initialised
ip_packet_type with following definition
static struct packet_type ip_packet_type = {
.type = __constant_htons(ETH_P_IP),
.func = ip_rcv,
};

And 2.4 have following definition.
static struct packet_type ip_packet_type =
{
__constant_htons(ETH_P_IP),
NULL, /* All devices */
ip_rcv,
(void*)1,
NULL,
};
Why . is used in 2.6 series kernels and not in 2.4 series?Also other
structure members are why not necessary to define in 2.6 kernel?
regards,
linux_lover.
 
M

Martin Ambuhl

linux.lover said:
Hello all,
I am analysing 2.4 and 2.6 series ip_output.c file code. What i
found is that 2.6 kernel initialised
ip_packet_type with following definition
static struct packet_type ip_packet_type = {
.type = __constant_htons(ETH_P_IP),
.func = ip_rcv,
^^ Are you sure this comma is there?
};

And 2.4 have following definition.
static struct packet_type ip_packet_type =
{
__constant_htons(ETH_P_IP),
NULL, /* All devices */
ip_rcv,
(void*)1,
NULL,
^^ Are you sure this comma is there?
};
Why . is used in 2.6 series kernels and not in 2.4 series?Also other
structure members are why not necessary to define in 2.6 kernel?

The 2.6 code (based on either the C99 standard or gcc, no doubt) allows
one to write initializations knowing only the names of the relevant
members of the struct. The 2.4 code (based on C90) requires one to know
the order of the members of the struct, including those that are not
relevant if they precede any of the relevant ones.

Note the members that you don't care about in the 2.4 code:
static struct packet_type ip_packet_type =
{
__constant_htons(ETH_P_IP), /* relevant */
NULL, /* NOT relevant */
ip_rcv, /* relevant */
(void*)1, /* NOT relevant */
NULL /* NOT relevant */
};

It appears that the (void *)1 for the fourth member is not needed as
more than a place holder and that the final NULL is silly. Even so, the
pruned down initialization requires that you know that you are
initializing the first and second elements:

static struct packet_type ip_packet_type =
{
__constant_htons(ETH_P_IP), /* relevant */
NULL, /* NOT relevant */
ip_rcv /* relevant */
};
 
S

S.Tobias

Martin Ambuhl said:
linux.lover wrote:
^^ Are you sure this comma is there?

There's nothing wrong with the trailing comma in a compound
initializer. See 6.7.8 syntax and examples 3 and 9.
 
M

Martin Ambuhl

S.Tobias said:
There's nothing wrong with the trailing comma in a compound
initializer. See 6.7.8 syntax and examples 3 and 9.

If you've been in comp.lang.c for more than 15 minutes, you know that I
tell people when something is wrong. I don't ask questions like the one
above. Silly twit.
 
S

S.Tobias

If you've been in comp.lang.c for more than 15 minutes, you know that I
tell people when something is wrong. I don't ask questions like the one
above. Silly twit.

:)
You had me doubt for a while.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top