본문으로 바로가기

이것 역시 리눅스 커널을 수정하다 struct의 좋은 예가 될 것 같아 옮겨봄.
역시 커널은 코딩스킬을 배울 수 있어 좋음.

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
struct at91_gpio_bank {
    unsigned short id;      /* peripheral ID */
    unsigned long offset;       /* offset from system peripheral base */
};
 
void at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
{
    int i;
    for (i = 0; i < nr_banks; i++) {
        struct at91_gpio_bank *bank = &data[i];
 
        printf("id: %d\n", bank->id);
        printf("offset: %d\n", bank->offset);
    }
}
 
 
void main(void)
{
    static struct at91_gpio_bank gpio[] = {
        {
            .id     = 0,
            .offset     = 0x123,
        }, {
            .id     = 1,
            .offset     = 0x124,
        }, {
            .id     = 2,
            .offset     = 0x125,
        }, {
            .id     = 3,
            .offset     = 0x126,
        }, {
            .id     = 4,
            .offset     = 0x127,
        }
    };
 
    at91_gpio_init(gpio, ARRAY_SIZE(gpio));
}