C語言結構體數組的初始化賦值可以通過以下兩種方式進行:
struct Student {
char name[20];
int age;
};
int main() {
struct Student students[3];
strcpy(students[0].name, "Tom");
students[0].age = 18;
strcpy(students[1].name, "Alice");
students[1].age = 20;
strcpy(students[2].name, "Bob");
students[2].age = 19;
return 0;
}
struct Student {
char name[20];
int age;
};
int main() {
struct Student students[3] = {
{"Tom", 18},
{"Alice", 20},
{"Bob", 19}
};
return 0;
}
通過上述兩種方式,可以對C語言中的結構體數組進行初始化賦值。