文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>结构体中数组成员的初始化方式

结构体中数组成员的初始化方式

时间:2009-03-11  来源:scutan

    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;
}

相关阅读 更多 +
排行榜 更多 +
特技摩托挑战(wheelie challenge)

特技摩托挑战(wheelie challenge)

赛车竞速 下载
创造世界游戏

创造世界游戏

冒险解谜 下载
终极躲避球

终极躲避球

休闲益智 下载