數組原來還可以這樣初始化【轉】

數組原來還可以這樣初始化:[0 ... LMAP_ENTRIES-1] = { "gliethttp

http://blog.chinaunix.net/u1/38994/showart_1334369.html

#include <stdio.h>
#include <string.h>

#define LMAP_ENTRIES 100
struct lmap {
    const char *name;
    void *data;
};
struct luther_gliethttp {
    struct lmap lmap[LMAP_ENTRIES];
    const char *name;
};

static struct luther_gliethttp luther = {
    .lmap = {
        [0 ... LMAP_ENTRIES-1] = { "gliethttp", NULL }
    },
    .name = "luther",
};
int main(int argc, char *argv[])
{
    int i;
    for (i = 0; i < LMAP_ENTRIES; i++) {
        printf("luther.lmap[%d].name = %s/n", i, luther.lmap[i].name);
    }
    printf("%p/n", ((unsigned int)(0x01 << ((sizeof(int) * 8) - 1)) - 1));
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章