易百教程

编写程序用来初始化结构数组

编写程序用来初始化结构数组,参考示例代码如下:

#include <stdio.h>

int main()
{
    struct president
    {
        char name[40];
        int year;
    } preslist[] = {
        { "George Washington",1789 },
        { "John Adams", 1797 }
    };

    printf("The first president of the USA was %s\\n", preslist[0].name);
    printf("He was inaugurated in the year %d\\n", preslist[0].year);
    printf("The second president of the USA was %s\\n", preslist[1].name);
    printf("He was inaugurated in the year %d\\n", preslist[1].year);
    system("pause");
    return(0);
}

执行上面示例代码,得到以下结果:

The first president of the USA was George Washington
He was inaugurated in the year 1789
The second president of the USA was John Adams
He was inaugurated in the year 1797