在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;
}
無論采用哪種方式,都可以根據具體需求來初始化結構體數組。