在C语言中,可以通过以下几种方式来初始化结构体数组:
struct Person {
char name[20];
int age;
};
int main() {
struct Person people[3];
strcpy(people[0].name, "Alice");
people[0].age = 20;
strcpy(people[1].name, "Bob");
people[1].age = 25;
strcpy(people[2].name, "Charlie");
people[2].age = 30;
return 0;
}
struct Person {
char name[20];
int age;
};
int main() {
struct Person people[3] = {
{"Alice", 20},
{"Bob", 25},
{"Charlie", 30}
};
return 0;
}
struct Person {
char name[20];
int age;
};
int main() {
struct Person people[3] = {
[0] = {"Alice", 20},
[1] = {"Bob", 25},
[2] = {"Charlie", 30}
};
return 0;
}
无论采用哪种方式,都可以根据具体需求来初始化结构体数组。