Chinaunix首页 | 论坛 | 博客
  • 博客访问: 635026
  • 博文数量: 54
  • 博客积分: 3812
  • 博客等级: 上校
  • 技术积分: 992
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-16 20:53
文章分类

全部博文(54)

文章存档

2010年(10)

2009年(24)

2008年(20)

分类: LINUX

2009-03-11 11:04:04

    Netfilter代码在初始化initial_table表的时候,针对里面的数组成员,使用了如下的初始化方式:
 

struct ebt_replace_kernel
{
    char name[EBT_TABLE_MAXNAMELEN];
    unsigned int valid_hooks;
    /* nr of rules in the table */
    unsigned int nentries;
    /* total size of the entries */
    unsigned int entries_size;
    /* start of the chains */
    struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
    /* nr of counters userspace expects back */
    unsigned int num_counters;
    /* where the kernel will put the old counters */
    struct ebt_counter *counters;
    char *entries;
};

 

static struct ebt_replace_kernel initial_table =
{
    .name        = "filter",
    .valid_hooks    = FILTER_VALID_HOOKS,
    .entries_size    = 3 * sizeof(struct ebt_entries),
    .hook_entry    = {
        [NF_BR_LOCAL_IN]    = &initial_chains[0],
        [NF_BR_FORWARD]        = &initial_chains[1],
        [NF_BR_LOCAL_OUT]    = &initial_chains[2],
    },
    .entries    = (char *)initial_chains,
};

    即在初始化数组的时候使用了[NF_BR_LOCAL_IN] = &initial_chains[0];的形式。可能有些朋友对这种方式还不太熟悉。

    下面是我以前写的一个相似的代码:

 

#include<stdio.h>
#include<stdlib.h>

struct A
{
    char name[10];
    int a1;
    int a2;
    int a3;
    int arr[5];
};

struct B
{
    struct A testa;
}structb =
{
    {
        "test1", 10, 20, 30,
        {
            [1] = 100,
            [2] = 200,
            [3] = 300,
            [4] = 400,
        }
    }
};

int main()
{
    int i;
    for(i = 0; i < 5; i++)
        printf("%d\n", structb.testa.arr[i]);
    return 0;
}

阅读(16457) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~