C語言結構體數組的賦值可以通過以下幾種方法實現:
struct Student {
int id;
char name[50];
};
int main() {
struct Student students[3];
for (int i = 0; i < 3; i++) {
students[i].id = i + 1;
sprintf(students[i].name, "Student %d", i + 1);
}
return 0;
}
struct Student {
int id;
char name[50];
};
int main() {
struct Student students[3] = {
{1, "Student 1"},
{2, "Student 2"},
{3, "Student 3"}
};
return 0;
}
#include <string.h>
struct Student {
int id;
char name[50];
};
int main() {
struct Student students1[3] = {
{1, "Student 1"},
{2, "Student 2"},
{3, "Student 3"}
};
struct Student students2[3];
memcpy(students2, students1, sizeof(students1));
return 0;
}
以上是常見的幾種方法,根據實際需求選擇合適的賦值方式。