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;
}
以上是常见的几种方法,根据实际需求选择合适的赋值方式。