C structure question

Discussion in 'C Programming' started by linux.lover, Jan 16, 2005.

  1. linux.lover

    linux.lover Guest

    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.
    linux.lover, Jan 16, 2005
    #1
    1. Advertising

  2. linux.lover wrote:
    > 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 */
    };
    Martin Ambuhl, Jan 16, 2005
    #2
    1. Advertising

  3. linux.lover

    S.Tobias Guest

    Martin Ambuhl <> wrote:
    > linux.lover wrote:


    > > static struct packet_type ip_packet_type = {
    > > .type = __constant_htons(ETH_P_IP),
    > > .func = ip_rcv,

    > ^^ 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.

    > > };


    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jan 16, 2005
    #3
  4. S.Tobias wrote:
    > Martin Ambuhl <> wrote:
    >
    >>linux.lover wrote:

    >
    >
    >>>static struct packet_type ip_packet_type = {
    >>> .type = __constant_htons(ETH_P_IP),
    >>> .func = ip_rcv,

    >>
    >> ^^ 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.


    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.
    Martin Ambuhl, Jan 17, 2005
    #4
  5. linux.lover

    S.Tobias Guest

    Martin Ambuhl <> wrote:
    >S.Tobias wrote:
    >> Martin Ambuhl <> wrote:
    >>>linux.lover wrote:


    >>>>static struct packet_type ip_packet_type = {
    >>>> .type = __constant_htons(ETH_P_IP),
    >>>> .func = ip_rcv,
    >>>
    >>> ^^ 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.


    > 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.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jan 17, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    745
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,205
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    599
  4. Replies:
    9
    Views:
    25,287
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    778
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page