要赋值C语言结构体数组,可以通过以下几种方式:
struct student {
char name[20];
int age;
};
int main() {
struct student arr[3];
strcpy(arr[0].name, "Tom");
arr[0].age = 20;
strcpy(arr[1].name, "Jerry");
arr[1].age = 22;
strcpy(arr[2].name, "Alice");
arr[2].age = 21;
return 0;
}
struct student {
char name[20];
int age;
};
int main() {
struct student arr[3] = {
{"Tom", 20},
{"Jerry", 22},
{"Alice", 21}
};
return 0;
}
struct student {
char name[20];
int age;
};
int main() {
struct student arr[3];
int i;
for (i = 0; i < 3; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", arr[i].name);
printf("请输入第%d个学生的年龄:", i + 1);
scanf("%d", &arr[i].age);
}
return 0;
}